My accepted c++ answer, is it necessary to check situations while moving the *fast at the first time?


  • 0
    K
     class Solution {
       public:
         ListNode* removeNthFromEnd(ListNode* head, int n) {
               if(!head) return NULL;
               ListNode* slow = head, *fast = head;
               while(n>0) {
                  fast = fast->next;
                  n--;
                if(!fast&&n==0)
                    return slow->next;
                else if(!fast&&n>0)
                    return NULL;
                else
                   continue;
               }
        
              while(fast->next) {
                 slow = slow->next;
                 fast = fast->next;
              }
        
             slow->next = slow->next->next;
             return head;   
    }
    

    };


Log in to reply
 

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