Remove Nth Node From End of List
You get the head of a linked list and a number n. Remove the n-th node counting from the end, where the last node is the 1st, and return the head of what is left. Our example: 7 → 3 → 8 → 2, n = 4 — the 4th from the end is the head 7, and the answer is 3 → 8 → 2.
- input
head: the head of a linked list;n: a position counted from its end, 1 for the last node.- returns
- The head of the list with its
n-th node from the end removed. - constraints
- 1 ≤ nodes ≤ 30
- 0 ≤ Node.val ≤ 100
- 1 ≤ n ≤ nodes
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
function removeNthFromEnd(head, n) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more fast and slow pointers problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.