Remove Nth Node From End of List
Medium
LC #19
two pointersdummy headNot attempted yet
Given the head of a linked list and an integer n,
remove the nth node counted from the end (n = 1 is
the last node) and return the head of the list.
Example 1
Input: head = [10,20,30,40,50], n = 2
Output: [10,20,30,50]
The 2nd node from the end is 40.
Example 2
Input: head = [4], n = 1
Output: []
Example 3
Input: head = [4,5], n = 2
Output: [5]
Constraints
- 1 ≤ number of nodes ≤ 5 · 10⁴
- 1 ≤ n ≤ number of nodes
- 0 ≤ Node.val ≤ 10⁵
Can you do it in a single pass?