Remove the nth node from the end
Standard · Write code · about 12 min
Given the head of a singly linked list and an integer n, remove the node that is n places from the end (n = 1 is the tail) and return the head. Try to do it in one pass.
Example 1
Input:
[[1,2,3,4,5],2]
Output:
[1,2,3,5]
4 is second from the end.
Example 2
Input:
[[1,2],2]
Output:
[2]
Removing the head.
Constraints
- 1 ≤ number of nodes ≤ 5,000
- 1 ≤ n ≤ number of nodes
- 0 ≤ node value ≤ 10,000
Learn the idea first: Singly linked lists: traverse, insert, delete, a visual lesson.
More Singly linked lists: traverse, insert, delete problems
- Insert after a node Warm-up
- Length of a linked list Warm-up
- Delete by value Standard
- Spot the bug: arrows set in the wrong order Standard
- Remove all duplicates from a sorted list Challenge