simple o(n)


  • 0
    L
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if(head==nullptr)   return head;
            ListNode* Head = new ListNode(0);
            ListNode* pHead = Head; 
            while(head)
            {
                if(head->next && head->next->val!= head->val || head->next==nullptr)
                {   pHead->next = new ListNode(head->val); pHead = pHead->next;}
                else
                {
                    while(head->next&&head->next->val== head->val)
                        head=head->next;
                }
                head=head->next;
            }
            pHead = Head->next; delete Head;
            return pHead;
        }
    };

Log in to reply
 

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