My short 8ms c++ solution


  • 0
    J
    class Solution {
      public:
        ListNode* deleteDuplicates(ListNode* head) {
          if(!head) return head;
          ListNode dummy(0), *p = &dummy; // use dummy node
          p->next = head;
    
          while( head->next ) {
            if(head->val != head->next->val) {
              if(p->next->next != head->next) {
                p->next = head->next;
              } else {
                p = p->next;
              }
            }
            head = head->next;
          }
          if(p->next->next) p->next = nullptr;
          
          return dummy.next;
        }
    };

Log in to reply
 

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