Short C++ iterative solution using dummy


  • 0
    S
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode newhead = ListNode(0);
            ListNode* dummy = &newhead,  *prev = NULL, *cur = head;
            while(cur)
            {
                if((prev  && (prev->val == cur->val)) || ( cur->next && (cur->val == cur->next->val)))
                    prev = cur;
                else
                   dummy = prev = dummy->next = cur;
                cur = cur->next;
            }
            dummy->next=NULL;//required consider 1->2->2 here dummy is 1 and cur is NULL but link is not broken
            return newhead.next;
        }
    

Log in to reply
 

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