Skip to content
mlmentorship

Keep a link gap until the leading pointer reaches the tail

Locate the predecessor of an end-relative target, preserve invalid-rank behavior, then switch to read-only lookup or front-index removal.

Published · 10 min read ·Core ·Intermediate

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

ML breadth · closed-book attempt

Keep a link gap until the leading pointer reaches the tail

Explain the mechanism, why it works, when it fails, and one alternative.

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.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

30-second answer map

Maintain a two-node gap so left stops immediately before the second node from the end.

Problem trace

A fixed pointer gap places left immediately before the node to remove

Input and goalRemove the `n`th node counted from the end.
Start both pointers at the dummyBoth pointers start at a dummy before the head. The same bypass can remove the head.
dummyleft→1→2→3→4→5
inputhead=[1,2,3,4,5], n=2rightdummygap0 nodes
input
head=[1,2,3,4,5], n=2
right
dummy
gap
0 nodes

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.
Both pointers start at a dummy before the head. The same bypass can remove the head. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why is left at node 3 when right reaches node 5? Which single link removes node 4?
Full interview answer 10 min read

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

Worked example: the dummy predecessor makes deleting the head an ordinary spliceThe dummy node gives even the head a predecessor. After locating the target, redirect the predecessor’s next link around it. Here that one splice makes the second node the new head. The same operation works when the target is inside the list.The dummy link can skip the head justlike any other targetBefore: D precedes the third nodefrom the endD123nullAfter D.next skips one; return headtwoD23null
Read it this way: The dummy node gives even the head a predecessor. After locating the target, redirect the predecessor’s next link around it. Here that one splice makes the second node the new head. The same operation works when the target is inside the list. Which next link changes when the removed node is the original head?
Reference drawing

Problem trace

A fixed pointer gap places left immediately before the node to remove

Worked example: the dummy predecessor makes deleting the head an ordinary spliceThe dummy node gives even the head a predecessor. After locating the target, redirect the predecessor’s next link around it. Here that one splice makes the second node the new head. The same operation works when the target is inside the list.The dummy link can skip the head justlike any other targetBefore: D precedes the third nodefrom the endD123nullAfter D.next skips one; return headtwoD23null
Execution trace
Input and goalRemove the `n`th node counted from the end.
Start both pointers at the dummyBoth pointers start at a dummy before the head. The same bypass can remove the head.
dummyleft→1→2→3→4→5
inputhead=[1,2,3,4,5], n=2rightdummygap0 nodes
input
head=[1,2,3,4,5], n=2
right
dummy
gap
0 nodes

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.
Both pointers start at a dummy before the head. The same bypass can remove the head. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why is left at node 3 when right reaches node 5? Which single link removes node 4?
Read it this way: The dummy node gives even the head a predecessor. After locating the target, redirect the predecessor’s next link around it. Here that one splice makes the second node the new head. The same operation works when the target is inside the list. Which next link changes when the removed node is the original 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.