Remove Nth Node From End
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.removeNthFromEnd(head, n). Remove the nth node from the end and return the list head. Use one traversal after creating a sentinel node.
Starter code
class Solution:
def removeNthFromEnd(self, head, n):
passTest cases
middle
{
"args": [
{
"$type": "linked-list",
"values": [
1,
2,
3,
4,
5
]
},
2
]
}Expected: {"$type":"linked-list","values":[1,2,3,5]}
Wizard outline
- Step 1: Initialize Solution.removeNthFromEnd
Replace the empty starter with the first real state owned by Solution.removeNthFromEnd. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the Middle case
Complete the readable core algorithm for one representative Interview case. Maintain a gap of n nodes between two pointers and unlink the target through its predecessor.
- Step 4: Harden the Remove Head boundary
Repair the reviewed boundary and pass the complete submission contract. The fixed gap makes slow stop immediately before the nth node from the end, so redirecting slow.next removes exactly that node; the sentinel also covers head removal.
Footguns and prerequisites
- A sentinel is necessary when the removed node is the original head.
- linked lists
Reviewed references
Practice prerequisites
- Relink Nodes Safely(opens in a new tab)
Relink Nodes Safely isolates every untouched node keeps its original next link, and at most one predecessor changes to the removed node’s former successor. That focused state discipline is required when implementing remove nth node from end as a complete Interview Problem.
- Trace Fast and Slow Pointers(opens in a new tab)
Trace Fast and Slow Pointers isolates after each loop, slow has advanced one link for every two links attempted by fast. That focused state discipline is required when implementing remove nth node from end as a complete Interview Problem.
Recommended approach and implementation
Place a sentinel before the head, advance fast by n + 1 positions, then move both pointers until fast reaches the end.
Why it works: The fixed gap makes slow stop immediately before the nth node from the end, so redirecting slow.next removes exactly that node; the sentinel also covers head removal.
class Solution:
def removeNthFromEnd(self, head, n):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
sentinel = ListNode(0, head)
slow = fast = sentinel
for _ in range(n + 1):
fast = fast.next
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return sentinel.next