Why Last executed input: [1], 1?


  • -1
    P

    leetcode Remove Nth Node From End of List

    The program can run successfully on my own compiler.

    but here shows Last executed input: [1], 1

    class Solution

    {
    public:

     ListNode* removeNthFromEnd(ListNode* head, int n)
    
     {
         if (head==NULL)
         
         {
            return NULL;
    
         }
    
         ListNode* ptmp = head;
         
         ListNode* pre = NULL;
         int count = 1;
         while (ptmp->next != NULL)
         {
             pre = ptmp;
             ptmp = ptmp->next; 
             count++;
         }
    
         if (n==1)
         {
            pre->next = NULL;
            delete ptmp;
         }
    
         else
         {
            
             ptmp = head;
             pre  = NULL;
             int tmp = count-n+1;
             
             if (tmp<0)
             {
                return NULL;
             }
              
             if (tmp == 1)
             {
                head == NULL;
                delete ptmp;
             }
            
             int num = 1;
             while (ptmp)
             {
                 num++;
                 pre = ptmp;
                 ptmp=ptmp->next;
                 if (num == tmp)
                 {
                     break;
                 }
                 
             }
          
                       
            pre->next = ptmp->next;
            delete ptmp;
    
         }
    
         return head;
    

    }
    };


Log in to reply
 

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