Given the head of a singly linked list and an integer n, delete the node that sits n positions from the end of the list and return the head of the modified list.
The counting is from the tail: n equal to 1 means the last node, n equal to 2 means the second-to-last, and so on. Aim to do this in a single pass over the list.
Example 1
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
The second node from the end holds value 4, so it is removed.
Example 2
Input: head = [1], n = 1
Output: []
Removing the only node from a one-element list yields an empty list.
Constraints
The number of nodes is in the range [1, 30].1 <= Node.val <= 1001 <= n <= number of nodesSee the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI