Skip to content
mlmentorship

Save the remaining chain before reversing the current link

Maintain disjoint reversed and untouched regions, preserve node identity, then copy the result or use bounded recursive control.

Published · 12 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

Save the remaining chain before reversing the current link

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

Problem

Input. The head of a finite acyclic singly linked list, with permission to change its next links. Output. Return the new head after reversing the original nodes' order. Rules. Reuse every node and retain its value; do not copy values into replacement nodes. The former head becomes the tail and must point to None. Repeated values are allowed. An empty list returns None; a singleton returns the same node. Example. 1 -> 2 -> 3 -> 4 -> None becomes 4 -> 3 -> 2 -> 1 -> None. The returned head is the original node valued 4.

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

Preserve the unreversed suffix before redirecting each current.next, then advance the previous/current boundary one node.

Problem trace

Save the next node before overwriting the forward link

Worked example: save the remaining chain before reversing the current linkBefore redirecting node 1 toward null, save its original next node 2. That saved reference is the only route to the unprocessed suffix.Save the suffix reference beforeredirecting the current linkOriginal chain; save the reference totwo123nullRedirect one toward the processedprefix1nullThe saved two still reaches theunprocessed suffix23null
Read it this way: Before redirecting node 1 toward null, save its original next node 2. That saved reference is the only route to the unprocessed suffix. Without saving two first, which reference would still reach the unprocessed suffix after one points to null?
Full interview answer 12 min read

Reverse the order of a linked list’s existing nodes by changing their next references.

Problem

Input. The head of a finite acyclic singly linked list, with permission to change its next links.

Output. Return the new head after reversing the original nodes’ order.

Rules. Reuse every node and retain its value; do not copy values into replacement nodes. The former head becomes the tail and must point to None. Repeated values are allowed. An empty list returns None; a singleton returns the same node.

Example. 1 -> 2 -> 3 -> 4 -> None becomes 4 -> 3 -> 2 -> 1 -> None. The returned head is the original node valued 4.

Picture

Maintain two disjoint regions containing every original node exactly once:

  • previous heads the reversed prefix.
  • head is the current node heading the untouched suffix.

Initially, the prefix is empty and the suffix contains all four nodes. Save node 2, set 1.next to None, then move previous to 1 and head to 2. After the next iteration, the regions are previous: 2 -> 1 -> None and head: 3 -> 4 -> None.

Redirecting the current node’s edge cannot lose the suffix because next_node still references its original successor. Moving both working references restores the same two-region invariant with one more reversed node. When head becomes None, previous heads the complete result.

Problem trace

Full list: advance the disjoint reversed-prefix and untouched-suffix boundary

Input and goalReverse all links in a singly linked list.
Initialize the two list regionsStart with an empty reversed prefix. The current reference points to the untouched four-node chain.
1current→2→3→4
previousnullactualLinks1->2, 2->3, 3->4, 4->null
previous
null
actual links
1->2, 2->3, 3->4, 4->null
node 0
1

Recognize it
Use it when a forward-only chain must reverse direction in constant extra space and changing current.next would otherwise destroy access to the unprocessed suffix.
Keep true
Before each iteration, previous heads a fully reversed prefix, head heads the untouched forward suffix, and together those disjoint regions contain every original node exactly once.
Reuse it
Before mutating the only route to remaining work, save that route; then rewire and advance the boundary. This applies to list splicing, segment reversal, and in-place pointer transformations.
Start with an empty reversed prefix. The current reference points to the untouched four-node chain. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Which reference still reaches nodes 2, 3 and 4 after 1.next is overwritten with null?
Reference drawing

Problem trace

Save the next node before overwriting the forward link

Worked example: save the remaining chain before reversing the current linkBefore redirecting node 1 toward null, save its original next node 2. That saved reference is the only route to the unprocessed suffix.Save the suffix reference beforeredirecting the current linkOriginal chain; save the reference totwo123nullRedirect one toward the processedprefix1nullThe saved two still reaches theunprocessed suffix23null
Execution trace
Input and goalReverse all links in a singly linked list.
Initialize the two list regionsStart with an empty reversed prefix. The current reference points to the untouched four-node chain.
1current→2→3→4
previousnullactualLinks1->2, 2->3, 3->4, 4->null
previous
null
actual links
1->2, 2->3, 3->4, 4->null
node 0
1

Recognize it
Use it when a forward-only chain must reverse direction in constant extra space and changing current.next would otherwise destroy access to the unprocessed suffix.
Keep true
Before each iteration, previous heads a fully reversed prefix, head heads the untouched forward suffix, and together those disjoint regions contain every original node exactly once.
Reuse it
Before mutating the only route to remaining work, save that route; then rewire and advance the boundary. This applies to list splicing, segment reversal, and in-place pointer transformations.
Start with an empty reversed prefix. The current reference points to the untouched four-node chain. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Which reference still reaches nodes 2, 3 and 4 after 1.next is overwritten with null?
Read it this way: Before redirecting node 1 toward null, save its original next node 2. That saved reference is the only route to the unprocessed suffix. Without saving two first, which reference would still reach the unprocessed suffix after one points to null?

Code

The platform supplies ListNode; use this definition when running all blocks together locally.

class ListNode:
   def __init__(self, val=0, next=None):
      self.val = val
      self.next = next
def reverse_list(head: ListNode | None) -> ListNode | None:
   previous = None

   while head:
      next_node = head.next
      head.next = previous
      previous = head
      head = next_node

   return previous

Each node crosses the boundary once, giving link work and extra references. The old head becomes the null-ended tail; payload values stay attached to their original nodes.

Failure Check

Writing head.next = previous before saving the successor can destroy the only route to the untouched suffix. Returning the exhausted head gives None, not the new head. Reversing twice must restore original identity order and all original links, not merely the same values.

Change to a Reversed Copy

New task: Preserve every original link and allocate an independent result chain.

Picture and change: Read the original forward, prepending one copied value to the new chain at each step.

def reversed_copy(head):
   copied = None
   while head:
      copied = ListNode(head.val, copied)
      head = head.next
   return copied

Check: The example returns new nodes with values [4,3,2,1]; the original still links [1,2,3,4]. No output node belongs to the input. Payloads are assigned, not recursively copied. Time remains linear; output allocation becomes nodes.

Use when: Input ownership forbids destructive rewiring.

Change to Recursive Control

New task: Express reversal through call-return structure, with safely bounded input depth.

Picture and change: Reverse the suffix first. Point the head’s original successor back to the head, then clear head.next.

def reverse_recursive(head):
   if head is None or head.next is None:
      return head
   new_head = reverse_recursive(head.next)
   head.next.next = head
   head.next = None
   return new_head

Check: [1,2,3] returns the same nodes as [3,2,1]; empty input returns None. Omitting head.next = None leaves a two-node cycle during unwinding. This implementation uses call-stack space and can exceed Python’s recursion limit.

Use when: A bounded chain and recursive reasoning justify the additional stack cost.

Transfer Check

What must an interleaving operation preserve before changing links in two independent chains?

Answer

Both unconsumed continuations. One saved successor protects one-chain reversal, but not two independently advancing sources.