Remove Nth Node From End of List
You are given the head of a linked list and an integer n. Remove the n-th node counted from the end of the list (the last node is the 1st from the end) and return the head of the resulting list.
Try to do it in one pass over the list.
Constraints
- The list has
sznodes,1 <= sz <= 30. 0 <= Node.val <= 1001 <= n <= sz
Examples
Example 1
Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
The 2nd node from the end holds 4. Unlink it: 3 now points to 5.
Example 2
Input: head = [1], n = 1 Output: []
Removing the only node leaves an empty list.
Example 3
Input: head = [1,2], n = 2 Output: [2]
n equals the length, so the head itself is removed.
JavaScript you'll use: Linked list and tree nodes
/**
* Linked list nodes are provided (you don't need to define them):
* class ListNode { constructor(val = 0, next = null) { this.val = val; this.next = next; } }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
function removeNthFromEnd(head, n) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.