Reorder List - Problem

Reorder List is a fascinating linked-list manipulation problem that challenges you to interweave nodes from opposite ends of the list.

Given the head of a singly linked-list, you need to transform it from its original form:
L₀ → L₁ → L₂ → ... → Lₙ₋₁ → Lₙ

Into this interleaved pattern:
L₀ → Lₙ → L₁ → Lₙ₋₁ → L₂ → Lₙ₋₂ → ...

Important constraints:

  • You cannot modify the values inside the nodes
  • You can only rearrange the node connections
  • The reordering must be done in-place

Example: If your list is [1,2,3,4,5], the result should be [1,5,2,4,3] - taking alternately from the start and end!

Input & Output

example_1.py — Basic Reordering
$ Input: head = [1,2,3,4]
Output: [1,4,2,3]
💡 Note: We alternate between the start and end: take 1 (start), then 4 (end), then 2 (next start), then 3 (next end). The pattern is L₀ → Lₙ → L₁ → Lₙ₋₁.
example_2.py — Odd Length List
$ Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
💡 Note: With 5 nodes, we get: 1 (first), 5 (last), 2 (second), 4 (second-last), 3 (middle). The middle element naturally ends up at the end of our reordered sequence.
example_3.py — Edge Case - Two Nodes
$ Input: head = [1,2]
Output: [1,2]
💡 Note: With only two nodes, taking first→last gives us the same order: 1→2. This is a base case where no actual reordering is needed.

Constraints

  • The number of nodes in the list is in the range [1, 5 × 104]
  • 1 ≤ Node.val ≤ 1000
  • You may not modify the values in the list's nodes. Only nodes themselves may be changed.

Visualization

Tap to expand
🃏 Card Shuffling TechniqueOriginal Deck:A♠2♥3♣4♦5♠Step 1: Split at MiddleA♠2♥3♣First Half4♦5♠Second HalfStep 2: Reverse Second HalfA♠2♥3♣5♠4♦ReverseStep 3: Interweave CardsA♠5♠2♥4♦3♣Final Result: A♠ → 5♠ → 2♥ → 4♦ → 3♣
Understanding the Visualization
1
Split the Deck
Find the exact middle and split into two equal (or nearly equal) halves
2
Flip Second Half
Reverse the order of cards in the second half
3
Interweave
Alternate taking one card from each half to create the final sequence
Key Takeaway
🎯 Key Insight: By reversing the second half first, we can easily alternate between the start and end elements during the merge phase, achieving perfect interleaving with O(1) space complexity.
Asked in
Amazon 45 Meta 38 Microsoft 32 Google 28
42.0K Views
High Frequency
~18 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen