TRT
Sign in
mediumFoundationsLeetCode#find the middle#reverse#interleave

Reorder list

Rearrange L0 → L1 → … → Ln into L0 → Ln → L1 → Ln−1 → … in place, by finding the middle, reversing the second half and merging.

The Runtime Theory Team1 min read
Taught inFast and slow pointersTwo pointers walking a list at different speeds find its middle, the node n from the end, and whether it loops back on itself, all in one pass with O(1) memory.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inhead = [1, 2, 3, 4]

out[1, 4, 2, 3]

inhead = [1, 2, 3, 4, 5]

out[1, 5, 2, 4, 3]

inhead = [1]

out[1]

Given a singly linked list L0 → L1 → … → Ln−1 → Ln, reorder it to L0 → Ln → L1 → Ln−1 → L2 → Ln−2 → … by changing pointers, not values.

Before you code

The pattern alternates between the front and the back, but a singly linked list can only walk forward. If the back half were reversed, it would also be walkable from its end. That splits the problem into three steps you already know.

Hints

  1. Find the middle with fast and slow pointers, and cut the list there.
  2. Reverse the second half.
  3. Interleave the two halves: one node from the first, one from the second, until the second runs out.
  4. Check both odd and even lengths; for odd lengths the middle node ends up last.

Production & complexity notes

When solving Reorder list on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Fast and slow pointers to reinforce the underlying pattern.

What it practices

Combining the middle-finding pointers with reversal, from Fast and slow pointers.

Not started

Sign in to save your learning progress.

Sign in to save