Skip to content
mlmentorship

Reorder List

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

Published · 5 min read ·Core ·Mixed

30-second answer map

Visual first · depth when needed

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

Preparing the visual…

ML breadth · active recall

Practice before you read

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

How practice works

ML breadth · closed-book attempt

Reorder List

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

08:00recommended time

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

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

Start with the concrete trace below. It shows the state the algorithm must carry as it runs.

Problem trace

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

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 / fast2345
guardfast.next=2 and fast.next.next=3

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.
Read it this way: 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.

Pattern: Find middle, reverse second half, then merge.

Simple idea: This problem combines three linked-list moves. Slow and fast pointers find the middle. Reverse the second half. Alternate nodes from the two halves.

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.