Super Simple and Easy to Understand Cpp Solution


  • 0
    Z
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode dummy(0);
            dummy.next = head;
            ListNode *p = &dummy;
            ListNode *p1 = head;
            ListNode *p2 = head;
            while (p1) {
                while (p2->next && p1->val==p2->next->val) {
                    p2 = p2->next;
                }
    
                if (p1==p2) {
                    p = p1;
                } else {
                    p->next = p2->next;
                }
    
                p1 = p2 = p->next;
            }
    
            return dummy.next;
    
        }
    };

Log in to reply
 

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