#143

Reorder List

Medium Middle + reverse + weave

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Reorder L0 → L1 → … → Ln into L0 → Ln → L1 → Ln−1 → … in place.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: first, last, second, second-last…

Why this works: split at the middle, reverse the second half, then weave the halves together.

Example: 1→2→3→4→5

halves 1→2→3 and 4→5 → reversed 5→4weave: 1→5→2→4→3

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Not cutting the first half (mid.next = null), which creates a cycle.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Linked List
🎬 Episodes 40–45: video coming soon