Whats wrong with my c++ code


  • 0
    L
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            if(head==NULL||n==0) return head;
            vector<ListNode*> temp;
            while(head){
                temp.push_back(head);
                head=head->next;
            }
            if(n>temp.size()) return temp[0];
            if(n==temp.size()) return temp[1];
            int size=temp.size();
            ListNode *pre=temp[size-n-1];
            ListNode *next=temp[size-n];
            //temp[size-n-1]->next=temp[size-n]->next;
            pre->next=next->next;
            return temp[0];
        }
    };
    

    Can't pass the case [1,1] 1, any ideas?!?!?!


  • 0
    T
    if(n==temp.size()) return temp[1];
    

    i guess u are not supposed to return temp[1] but temp[0]->next


Log in to reply
 

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