Remove Nth Node From End of List
1 approach, code in every language
Problem description
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 nodes
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Unhook the Nth Wagon from the Tail is our own problem on the technique behind Remove Nth Node From End of List: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free