Data Structures & Algorithms Linked Lists
💡
Exercise 67

Reorder List 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given a linked list L0 → L1 → … → Ln-1 → Ln, reorder it to L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

# Before: 1 → 2 → 3 → 4 → 5 # After: 1 → 5 → 2 → 4 → 3

This combines three classic techniques:

  • Find middle: Use slow/fast pointers
  • Reverse: Reverse the second half
  • Merge: Interleave the two halves

Time: O(n), Space: O(1). This problem tests whether you truly understand linked list manipulation.

📋 Instructions
Implement `reorder_list(head)` using the three-step approach. Reorder [1,2,3,4,5] and print all values.
Step 1: Find middle with slow/fast. Step 2: Reverse the second half (from slow.next). Step 3: Merge first half and reversed second half by alternating nodes.
🧪 Test Cases
Input
Run your code
Expected
1 5 2 ...
Expected output (5 lines)
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.