My 8ms solution in C++


  • 0
    P
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) 
        {
            ListNode* tmp = new ListNode(0);
            ListNode *p1, *p2;
            
            tmp->next = head;
            
            p1 = tmp;
            p2 = p1->next;
            while(p2)
            {
                while(p2->next && p2->next->val==p2->val)
                    p2 = p2->next;
                    
                /*duplicate*/
                if(p1->next != p2)
                {
                    p2 = p2->next;
                    p1->next = p2;
                }
                /*not duplicate*/
                else
                {
                    p1 = p2;
                    p2 = p2->next;
                }
            }
            
            ListNode* ret = tmp->next;
            delete tmp;
            return ret;
        }
    };

Log in to reply
 

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