Much easy with 8ms c++ solution


  • 1
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if(!head||!head->next)return head;
            ListNode *p=new ListNode(0);
            ListNode *newhead=p;
            int tmp;
    
            while(head&&head->next){
              if(head->val!=head->next->val){
                  p->next=head;
                  p=p->next;
                  head=head->next;
              }
              else{
                  tmp=head->val;
                  while(head&&tmp==head->val)
                  head=head->next;
              }
            }
    
            p->next=head;
            return newhead->next;
        }
    };

  • 0

    Really easy-understanding.


Log in to reply
 

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