ONE PASS solution using javascript


  • 0
    L

    use recursion. code here:

    var removeNthFromEnd = function (head, n) {
        function f(x) {
            if (x === null)
                return 0;
            var t = x.next;
            var tn = f(t);
            if (tn === n)
                x.next = t.next;
    
            return tn + 1;
        }
        if (f(head) === n)
            head = head.next;
        return head;
    };
    

Log in to reply
 

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