I think this solution should completely work, but it gets a runtime error. WHY


  • 0
    B
    class Solution {
    public:
        ListNode *removeNthFromEnd(ListNode *head, int n) {
            if(!head)
                return 0;
            
            vector<ListNode*> list;
            ListNode* curr = head;
            int i = 0;
            list[i] = curr;
            while(curr->next){
                i++;
                curr = curr->next;
                list[i] = curr;
            }
            i++; 
            
            if((i - n) == 0){
                return head->next;
            }
            
            list.at(i - n - 1)->next = list.at(i - n)->next;
            return head;
        }
    };

  • 0
    N

    For vector, you may need to use push_back instead of directly access.


Log in to reply
 

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