Remove the node at a specified one-based position counted backward from a linked list’s end.
Problem
Input. The head of a finite acyclic linked list and an integer rank n counted from the end.
Output. Return the head of the list after removing that occurrence, which may be a different head.
Rules. Rank 1 means the tail; rank equal to the list length means the head. Rewrite links among retained original nodes rather than copying their values into new nodes. The reference leaves the chain unchanged for nonpositive or oversized ranks, including an empty chain.
Example. Removing rank 2 from 1 -> 2 -> 3 -> 4 -> 5 returns 1 -> 2 -> 3 -> 5.
Removing rank 1 from a one-node list returns None.
Picture
Start left and right at a dummy before the head.
Advance right by n links, then move both references one link per round.
Their gap stays exactly n links.
For n=2, their positions become (dummy,2), (1,3), (2,4), then (3,5).
Stop when right.next is missing: right is at the tail, not beyond it.
Now left.next is the target, node 4.
For length , the tail is at position after the dummy’s position zero.
The gap places left at , immediately before position .
Assigning left.next = left.next.next bypasses that target.
The dummy also supplies a predecessor when the target is the original head.
Problem trace
Separate head-removal case: the dummy owns the bypass link
Reference drawing
Problem trace
A fixed pointer gap places left immediately before the node to remove
Execution trace
- input
- head=[1,2,3,4,5], n=2
- right
- dummy
- gap
- 0 nodes
- gap
- 0 nodes→1 node
- right
- dummy→not shown
- gap
- 1 node→2 nodes
- target
- not shown→left.next is node 4
- changed link
- not shown→3.next: node 4 -> node 5
- removed
- not shown→node 4
- result
- not shown→[1,2,3,5]
- Recognize it
- The node is identified relative to the list end, but only one forward pass is allowed, so a leading pointer can convert distance-from-end into a simultaneous stopping position.
- Keep true
- After the initial advance, right remains exactly n links ahead of left. Therefore, when right is the tail, left.next is the nth node from the end.
- Reuse it
- Use a fixed lead whenever a linked-list target is defined by distance from the end; add a dummy when the operation may change the head.
Code
The platform supplies ListNode; this definition supports all blocks when running them together locally.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def remove_nth_from_end(head: ListNode | None, n: int) -> ListNode | None:
dummy = ListNode(0, head)
left = dummy
right = dummy
for _ in range(n):
if right.next is None:
return head
right = right.next
while right.next:
left = left.next
right = right.next
if left.next:
left.next = left.next.next
return dummy.next
Let denote list length, distinct from rank n.
The function performs link work using extra node references.
Empty input returns None.
Failure Check
Changing the initial gap to n+1 without changing the stopping rule selects the wrong predecessor.
Bypassing a node removes it from the returned chain; it does not destroy external references or clear its outgoing link.
Change to Read-Only Lookup
New task: Return the nth value from the end without changing links.
Picture and change: Lead by rank links from the head, then walk until the lead reaches None.
No predecessor is needed, so both the starting and stopping conventions change.
def nth_value_from_end(head, rank):
if rank <= 0:
return None
lead = head
for _ in range(rank):
if lead is None:
return None
lead = lead.next
lag = head
while lead:
lead, lag = lead.next, lag.next
return lag.val
Check: The example returns 4 with all links unchanged; invalid ranks return None.
If None is a valid payload, choose a different missing-result contract.
Use when: The end-relative query is read-only.
Change to a Front Index
New task: Remove a node identified by its zero-based front index.
Picture and change: Count length, convert index to end rank length-index, then call the reference above.
def remove_front_index(head, index):
length, node = 0, head
while node:
length += 1
node = node.next
if not 0 <= index < length:
return head
return remove_nth_from_end(head, length - index)
Check: Index 0 on [1,2,3] maps to rank 3 and returns the original 2 -> 3 suffix.
Index 4 leaves that input unchanged.
This adapter makes two passes; both variants retain linear link work and constant extra references.
Use when: The interface supplies front-relative coordinates.
Transfer Check
Why does removal start at a dummy while value lookup starts at the head?
Answer
Removal needs an incoming link to change, even for the head. Lookup needs the target itself, so it uses a different boundary convention.