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.
Visualization
Tap to expand
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.
Time & Space Complexity
Time Complexity
O(n)
Single pass to collect nodes plus O(n) to reconnect them
✓ Linear Growth
Space Complexity
O(n)
Array to store all n nodes
⚡ Linearithmic Space
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code