C++ 3-Lines Iterative, O(N) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            for (ListNode *cur = head, *prev = 0; cur; cur = cur->next)
                if (prev && cur->val == prev->val) prev->next = cur->next; else prev = cur;
            return head;
        }
    };
    

Log in to reply
 

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