Very Simple code but have a wrong answer when input{8,8}


  • 0
    J
    class Solution
     {
      public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        ListNode **pcur=&head;
        while((*pcur)->next!=NULL)
        {
            if((*pcur)->val==(*pcur)->next->val)
            {
                *pcur=(*pcur)->next;
    
            }
            else
            {
                pcur=&((*pcur)->next);
    
            }
        }
        return head;
    }
     };
    

    When the list is{1,1,2,3,3,3,4,4,5,6,6},the output result is {1,1,2,3,4,5,6},why?


  • 0
    L

    I think your solution cannot handle the case where the proceeding numbers are duplicates, e.g. {1, 1, 1}, or {2, 2, 3, 4}. Because of your if condition.


Log in to reply
 

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