My C++ solution, O(n)


  • 0
    A
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dum = new ListNode(0);
        dum->next = head;
        head = dum;
        int v;
        while (dum->next) {
            if (dum->next->next && dum->next->next->val == dum->next->val) {
                v = dum->next->val;
                while (dum->next && dum->next->val == v) {
                    dum->next = dum->next->next;
                }
            } else {
                dum = dum->next;
            }
        }
        return head->next;
    }

Log in to reply
 

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