Simple C++ solution, using a probe


  • 0
    I
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head) return NULL;
        ListNode *dummy = new ListNode(0), *cur = dummy;
        dummy->next = head;
        ListNode *probe = cur->next->next;
        while (probe) {
            if (cur->next->val == probe->val) {
                while (probe && probe->val == cur->next->val) probe = probe->next;
                cur->next = probe;
                if (probe) probe = probe->next;
            } else {
                cur = cur->next;
                probe = probe->next;
            }
        }
        return dummy->next;
    }

Log in to reply
 

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