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
Reorder List - Optimal Solution INPUT Original Linked List: 1 L₀ 2 L₁ 3 L₂ 4 L₃ NULL head = [1, 2, 3, 4] Transform Pattern: L0 --> L1 --> L2 --> L3 becomes L0 --> L3 --> L1 --> L2 Constraints: - Cannot modify node values - Only rearrange connections - Must be in-place ALGORITHM STEPS 1 Find Middle Use slow/fast pointers slow=1, fast=1 mid = node 2 2 Reverse Second Half Reverse list from middle First: 1 --> 2 Second: 3 --> 4 Reversed: 4 --> 3 3 Merge Two Halves Interleave alternately L1: 1 --> 2 L2: 4 --> 3 Merge: 1-->4-->2-->3 4 Update Pointers Reconnect all nodes Time: O(n) | Space: O(1) Optimal in-place solution FINAL RESULT Reordered Linked List: 1 start 4 end 2 start 3 end NULL Output: [1, 4, 2, 3] OK - Reordered! Pattern verified: L0-->L3-->L1-->L2 Key Insight: The optimal approach uses three key techniques: (1) Find the middle using slow/fast pointers, (2) Reverse the second half in-place, and (3) Merge by interleaving nodes alternately. This achieves O(n) time and O(1) space - no extra data structures needed! TutorialsPoint - Reorder List | Optimal Three-Step Solution
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