A concise O(N) solution


  • 1
    S
    ListNode *deleteDuplicates(ListNode *head) {
        if (NULL == head) return head;
        ListNode *cur = head, *nxt = head->next;
        
        while (nxt) {
            // find the next different one
            while (nxt && (cur->val == nxt->val))
                nxt = nxt->next;
            cur = cur->next = nxt;
            if (nxt) nxt = nxt->next;
        }
        
        return head;
    }
    
     /* modified to a more concise one */
    ListNode *deleteDuplicates(ListNode *head) {
        if(NULL == head) return head;
        
        ListNode *cur = head, *nxt = head->next;
        while (nxt) {
            if (cur->val != nxt->val) 
                cur = cur->next = nxt;
            else if (!nxt->next)
                cur->next = nxt->next;
            nxt = nxt->next;
        }
        
        return head;
    }

  • 0
    B

    You can make it more concise by eliminating the inner while loop. Can you think how?


  • 0
    S

    Yes, i have changed it.


Log in to reply
 

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