very strange test case cause a runtime error,anyone can help?


  • 0
    R

    Remove Nth Node From End of List
    when i click submit solution, it will cause a runtime error,but it's totally ok for run code button !!!
    case is below:
    [3,7,9,3,5,8,0]
    1

    my code :

    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            if(head==NULL)return head;
            ListNode* fast=head;
            ListNode* slow=head;
            while((n--)>1)fast=fast->next;//让fast提前开始和slow拉开N步
            while(fast->next!=NULL)
            {
                fast=fast->next;
                slow=slow->next;
            }
            if(slow==head)
            {
                head=head->next;
                return head;
            }
            fast=head;
            while(fast->next!=slow)fast++;
            fast->next = slow->next;
            return head;
        }
    };
    

Log in to reply
 

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