A 84ms solution in Javascript


  • 1
    B
    var removeNthFromEnd = function(head, n) {
        var dummy = new ListNode(0);
        dummy.next = head;
        var temp = dummy;
        var i = 0;
        while(temp !== null && i < n) {
            temp = temp.next;
            i++;
        }
        if(temp === null) return dummy.next;
        var slow = dummy;
        while(temp.next !== null) {
            temp = temp.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    };

Log in to reply
 

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