Skip to content
mlmentorship

Reorder List

Change `1, 2, 3, 4, 5` into `1, 5, 2, 4, 3`.

Published · 14 min read ·Core ·Mixed

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

Reorder List

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

Problem

Input. The head of a finite acyclic integer linked list. Output. Rewire the same nodes in place into first, last, second, second-last order, continuing inward; return None. Rules. Use every original node exactly once, retain its value, and end the reordered list with None. Do not create a replacement output chain or merely exchange node values. Values may repeat and need not be sorted. Empty and singleton lists remain unchanged. Example. 1 -> 2 -> 3 -> 4 -> 5 becomes 1 -> 5 -> 2 -> 4 -> 3. The four-node list 1 -> 2 -> 3 -> 4 becomes 1 -> 4 -> 2 -> 3.

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

Find the first-half tail, reverse the detached second half, then splice one reversed node after each first-half node.

Problem trace

Split and terminate before reversing and weaving the two halves

Worked example: reverse the detached second half before weavingSplit after node 3 and reverse the detached second half. Alternating the detached halves yields (1,5,2,4,3) without a cycle.Detach and reverse the second halfbefore weavingOriginal list12345nullFirst half stops after three123nullDetached second half reverses tofive, four54nullWeave without reconnecting the oldmiddle edge15243null
Read it this way: Split after node 3 and reverse the detached second half. Alternating the detached halves yields (1,5,2,4,3) without a cycle. What old link must be removed before alternating the two halves?
Full interview answer 14 min read
Reading depth

Rearrange a linked list into alternating nodes from its beginning and end without replacing its nodes.

Problem

Input. The head of a finite acyclic integer linked list.

Output. Rewire the same nodes in place into first, last, second, second-last order, continuing inward; return None.

Rules. Use every original node exactly once, retain its value, and end the reordered list with None. Do not create a replacement output chain or merely exchange node values. Values may repeat and need not be sorted. Empty and singleton lists remain unchanged.

Example. 1 -> 2 -> 3 -> 4 -> 5 becomes 1 -> 5 -> 2 -> 4 -> 3. The four-node list 1 -> 2 -> 3 -> 4 becomes 1 -> 4 -> 2 -> 3.

Picture

For 1->2->3->4->5, the first half is 1->2->3; the detached second half is 4->5. Reverse it to 5->4, then alternate to obtain 1->5->2->4->3.

Problem trace

Split and terminate before reversing and weaving the two halves

Input and goalChange `1, 2, 3, 4, 5` into `1, 5, 2, 4, 3`.
Initialize middle pointersFor 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist.
1slow / fast→2→3→4→5
guardfast.next=2 and fast.next.next=3
guard
fast.next=2 and fast.next.next=3
node 0
1
node 1
2

Recognize it
Use it when nodes must alternate from the front and back of a singly linked list without an auxiliary array or stack.
Keep true
The midpoint scan keeps fast twice as far along; reversal preserves a reversed prefix plus untouched suffix; merging preserves the final alternating prefix and two unconsumed chains.
Reuse it
Complex list transforms become safe compositions of small invariants: locate a boundary, detach, reverse with a saved route, then splice only after saving both continuations.
For 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why must 3.next become null before weaving? Where does the odd middle node end up?
Reference drawing

Problem trace

Split and terminate before reversing and weaving the two halves

Worked example: reverse the detached second half before weavingSplit after node 3 and reverse the detached second half. Alternating the detached halves yields (1,5,2,4,3) without a cycle.Detach and reverse the second halfbefore weavingOriginal list12345nullFirst half stops after three123nullDetached second half reverses tofive, four54nullWeave without reconnecting the oldmiddle edge15243null
Execution trace
Input and goalChange `1, 2, 3, 4, 5` into `1, 5, 2, 4, 3`.
Initialize middle pointersFor 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist.
1slow / fast→2→3→4→5
guardfast.next=2 and fast.next.next=3
guard
fast.next=2 and fast.next.next=3
node 0
1
node 1
2

Recognize it
Use it when nodes must alternate from the front and back of a singly linked list without an auxiliary array or stack.
Keep true
The midpoint scan keeps fast twice as far along; reversal preserves a reversed prefix plus untouched suffix; merging preserves the final alternating prefix and two unconsumed chains.
Reuse it
Complex list transforms become safe compositions of small invariants: locate a boundary, detach, reverse with a saved route, then splice only after saving both continuations.
For 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why must 3.next become null before weaving? Where does the odd middle node end up?
Read it this way: Split after node 3 and reverse the detached second half. Alternating the detached halves yields (1,5,2,4,3) without a cycle. What old link must be removed before alternating the two halves?

The slow pointer leaves the first half at least as long as the second. Detachment prevents an old middle link from forming a cycle. Save both successors before each splice, so every unprocessed node remains reachable.

Code

Define the platform node type before the unchanged reference.

class ListNode:
   def __init__(self, val: int = 0, next_node: "ListNode | None" = None):
      self.val = val
      self.next = next_node
def reorder_list(head: ListNode | None) -> None:
   if head is None or head.next is None:
      return

   slow = head
   fast = head
   while fast.next and fast.next.next:
      slow = slow.next
      fast = fast.next.next

   second = slow.next
   slow.next = None
   previous = None
   while second:
      next_node = second.next
      second.next = previous
      previous = second
      second = next_node

   first = head
   second = previous
   while second:
      first_next = first.next
      second_next = second.next
      first.next = second
      second.next = first_next
      first = first_next
      second = second_next

Cost: time and space.

The platform supplies ListNode with val and next; this snippet assumes that definition.

Change the Requirement

Preserve the Input

New task: Return a reordered copy without changing original links.

Picture and change: Copy in original order, then reuse the complete reference on the copy.

Code: Use ListNode above.

def reordered_copy(head: ListNode | None) -> ListNode | None:
   dummy = tail = ListNode()
   while head:
      tail.next = ListNode(head.val)
      tail = tail.next
      head = head.next
   reorder_list(dummy.next)
   return dummy.next

Check: 1->2->3 yields fresh 1->3->2 nodes while the input stays 1->2->3.

Use when: External references require the original topology; result allocation becomes .

Undo the Reordering

New task: Recover the original order after the stated transformation.

Picture and change: Keep alternating first-half nodes; prepend alternating second-half nodes, then join both chains.

Code: No node allocation is required.

def undo_reorder(head: ListNode | None) -> None:
   first = head
   second_reversed = None
   while first and first.next:
      second = first.next
      first.next = second.next
      second.next = second_reversed
      second_reversed = second
      if first.next is None:
         break
      first = first.next
   if first:
      first.next = second_reversed

Check: 1->5->2->4->3 returns to 1->2->3->4->5.

Use when: The input was produced by the forward transformation. Time remains linear and workspace constant.

Reconstruct

What happens if slow.next = None is omitted?

Reasoned answer

For 1->2->3, node two retains its old link to node three. The alternating splice then links node three back to node two, forming a cycle. Reversal alone does not establish disjoint ownership of the two chains. Detaching is part of the proof, not optional cleanup.