Share my simple C++ code


  • 0
    L
     ListNode* deleteDuplicates(ListNode* head) 
        {
           ListNode *cur=head,*pre=nullptr,*old=nullptr;
               while(cur)
               {
                   old=cur->next;
                   while(cur->next&&cur->val==cur->next->val)cur->next=cur->next->next;
                   if(old!=cur->next)
                   {
                       if(pre)pre->next=cur->next;
                       else head=cur->next;
                   }
                   else
                   pre=cur;
                   cur=cur->next;
               }
               return head;   
        }

Log in to reply
 

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