My C++ solution, the runtime error is solved


  • 1
    W

    The head is pointed to the first node with a value

    class Solution {
        public:
            ListNode *removeNthFromEnd(ListNode *head, int n) {
                vector<ListNode*> pointers;
                ListNode *thisnode;
                pointers.push_back(head);
                thisnode=head->next;
                int count=1;
                //search the list and store every node by pointer
                while(thisnode!=NULL)  
                {
                    pointers.push_back(thisnode);
                    thisnode=thisnode->next;
                    count++;
                }
                //delete the node
                if(count==n)
                    return pointers[0]->next;
                else
                    (pointers[count-n-1])->next=pointers[count-n]->next;
                
                return pointers[0];
            }
        };

Log in to reply
 

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