C++ , no memery leak ,accepted


  • 0
    F
    • list item
      '''

    class Solution {
    public:
    ListNode* deleteDuplicates(ListNode* head) {
    if(head == NULL || head->next == NULL)
    {
    return head;
    }
    ListNode* dup;
    ListNode* newhead = new ListNode(head->val -1);
    newhead->next = head;
    ListNode* pre = newhead;
    ListNode* phead = head;
    ListNode* next = head->next;
    bool isdup = false;

      while(phead != NULL && phead->next != NULL)
      {
          if(phead->val == phead->next->val)
          {
              while(phead != NULL && phead->next != NULL && phead->val == phead->next->val)
              {
                  dup = phead->next;
                  if(phead->next->next != NULL)
                  {
                      phead->next = phead->next->next;
                  }
                  else
                  {
                      phead->next = NULL;
                  }
                  delete dup;
                  dup = NULL;
                  isdup = true;
              }
              
              dup = phead;
              phead = phead->next;
              pre->next = phead; 
              delete dup;
              dup = NULL;
          }
          else
          {
              pre = pre->next;
              phead = phead->next;
          }
      }
       dup = newhead->next;
       delete newhead;
       return dup;
    }
    

    strikethrough text
    '''

    };


Log in to reply
 

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