Clean C++ Code without dummy head


  • 1
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode *p = head, *pre = NULL;
            while(p != NULL)
            {
                ListNode *q = p->next;
                if(q && q->val == p->val)
                    while(q && q->val == p->val)
                        q = q->next;
                else
                {
                    if(pre)
                        pre->next = p;
                    else 
                        head = p;
                    pre = p;
                }
                p = q;
            }
            if(!pre)
                return pre;
            pre->next = NULL;
            return head;
        }
    

Log in to reply
 

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