One pass 3ms C++ solution


  • 0
    R
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
            if(n == 0) return head;
            
            ListNode temp = ListNode(100);
            ListNode* p = &temp;
            p->next = head;
            onePassFindN(p, n);
            return p->next;
        }
        int onePassFindN(ListNode* l, int n){
            if(!l)
                return 0;
            int i;
            if((i = onePassFindN(l->next, n)) == n)
                l->next = l->next->next;
            return 1+i;
        }
    };
    

Log in to reply
 

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