6 ms C++ Solution


  • 0
    H
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if (!head) return NULL;
            ListNode node0(0);
            node0.next = head;
            ListNode *pre = &node0, *cur = head, *post = head->next;
            while (post != NULL) {
                if (cur->val == post->val) {
                    int val = cur->val;
                    while (cur && cur->val == val) {
                        ListNode *temp = cur;
                        cur = cur->next;
                        delete(temp);
                    }
                    if (cur) {
                        pre->next = cur;
                        post = cur->next;
                    } else {
                        pre->next = NULL;
                        return node0.next;
                    }
                } else {
                    pre = pre->next;
                    cur = cur->next;
                    post = post->next;
                }
            }
            return node0.next;
        }
    };
    

Log in to reply
 

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