Pretty simple C++ solution. 20 lines


  • 0
    D
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head){
            if(head == nullptr || head->next == nullptr) return head;
            
            ListNode *dummy = new ListNode(-1);
            dummy->next = head;
            ListNode *left = dummy, *right = head;
            
            while(right != nullptr){
                while(right->next != nullptr && right->val == right->next->val) right = right->next;
                if(left->next != right) left->next = right->next;
                else left = left->next;
                right = right->next;
            }
            
            return dummy->next;
        }
    };

Log in to reply
 

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