#328

Odd Even Linked List

Medium Two chains, then join

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

?The problem

Reorder a linked list so all nodes at odd positions (1st, 3rd, …) come first, followed by nodes at even positions, keeping relative order. O(1) extra space.

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

◎Understanding

Really asking: all odd-position nodes first, then even-position nodes.

Why this works: build two chains while walking, then attach the even chain after the odd one.

Example: 1→2→3→4→5

odd: 1→3→5, even: 2→41→3→5→2→4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Losing the even chain's head, so it can't be attached at the end.

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