C++ Simple method (iterative)


  • 0
    S
    ListNode* deleteDuplicates(ListNode* head) 
    {
        ListNode* lastNode= head;
        ListNode* now;
        
        if(head!=NULL) 
             now= head->next;
        while(now!=NULL)
        {
            if(now->val== lastNode->val)
            {
                lastNode->next= now->next;  //  (n-1) node points to (n+1) node.
                delete now; //release memory
            }    
            else
                lastNode= now;
                
            now= lastNode->next; 
        }
        return head;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.