LeetCode ยท #143

Reorder List Solution

Reorder Lโ‚€โ†’Lโ‚โ†’โ€ฆโ†’Lโ‚™ to Lโ‚€โ†’Lโ‚™โ†’Lโ‚โ†’Lโ‚™โ‚‹โ‚โ†’Lโ‚‚โ†’Lโ‚™โ‚‹โ‚‚โ†’โ€ฆ in-place.

01
Section One ยท Problem

Problem Statement

๐Ÿท๏ธ

Difficulty

Medium

๐Ÿ”—

LeetCode

Problem #143

๐Ÿ—๏ธ

Pattern

Fast-Slow + Reverse + Merge

Given the head of a singly linked list Lโ‚€โ†’Lโ‚โ†’โ€ฆโ†’Lโ‚™โ‚‹โ‚โ†’Lโ‚™, reorder it to Lโ‚€โ†’Lโ‚™โ†’Lโ‚โ†’Lโ‚™โ‚‹โ‚โ†’Lโ‚‚โ†’Lโ‚™โ‚‹โ‚‚โ†’โ€ฆ. You may not modify node values โ€” only change the node links.

Example
Input: 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5 Output: 1 โ†’ 5 โ†’ 2 โ†’ 4 โ†’ 3
Constraints
โ€ข 1 โ‰ค number of nodes โ‰ค 5 ร— 10โด โ€ข 1 โ‰ค Node.val โ‰ค 1000
02
Section Two ยท Approach 1

Array + Rebuild โ€” O(n) space

Store all nodes in an array. Use two pointers (front and back) to relink in the desired order. Correct but O(n) extra space.

Problem: O(n) space for the array. We can achieve O(1) space by combining three linked-list primitives: find-middle, reverse, and merge.
03
Section Three ยท Approach 2

Three-Step In-Place โ€” O(n) time, O(1) space

The reordered list interleaves the first half with the reversed second half. Three steps: (1) find the middle, (2) reverse the second half, (3) merge alternating.

๐Ÿ’ก Mental model: Take a deck of cards 1-2-3-4-5. Split it at the middle: [1,2,3] and [4,5]. Reverse the second pile: [5,4]. Now interleave: take from first, then second โ€” 1,5,2,4,3. That's the reordered list.
Algorithm
  • Step 1 โ€” Find middle: Use slow/fast pointers. When fast reaches the end, slow is at the middle.
  • Step 2 โ€” Reverse second half: Reverse the list starting from slow.next. Cut the first half by setting slow.next = null.
  • Step 3 โ€” Merge alternating: Take one node from the first half, then one from the reversed second half, alternating.
๐ŸŽฏ Three classic sub-problems combined:
  • This problem is a composition of LC 876 (Middle of Linked List), LC 206 (Reverse Linked List), and LC 21 (Merge Two Sorted Lists) โ€” but with alternating instead of sorted merge.
  • Master each primitive first.
04
Section Four ยท Trace

Visual Walkthrough

Trace: 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5:

Three-step reorder โ€” split, reverse, merge
ORIGINAL: 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5 1 โ†’ 2 โ†’ 3 โ†’ 4 โ†’ 5 โ†‘ mid โ‘  Find middle (slow/fast): slow lands at 3. First half: 1โ†’2โ†’3 | Second half: 4โ†’5 โ‘ก Reverse second half: 4โ†’5 becomes 5โ†’4 Now: first=1โ†’2โ†’3โ†’null, second=5โ†’4โ†’null โ‘ข Merge alternating: Take 1, take 5, take 2, take 4, take 3 RESULT 1 โ†’ 5 โ†’ 2 โ†’ 4 โ†’ 3 Answer: 1โ†’5โ†’2โ†’4โ†’3 โœ“
05
Section Five ยท Implementation

Code โ€” Java & Python

Java โ€” Three-step in-place
class Solution { public void reorderList(ListNode head) { if (head == null || head.next == null) return; // Step 1: Find middle ListNode slow = head, fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } // Step 2: Reverse second half ListNode second = slow.next; slow.next = null; // cut first half ListNode prev = null; while (second != null) { ListNode next = second.next; second.next = prev; prev = second; second = next; } second = prev; // new head of reversed half // Step 3: Merge alternating ListNode first = head; while (second != null) { ListNode tmp1 = first.next, tmp2 = second.next; first.next = second; second.next = tmp1; first = tmp1; second = tmp2; } } }
Python โ€” Three-step in-place
class Solution: def reorderList(self, head: Optional[ListNode]) -> None: if not head or not head.next: return # Step 1: Find middle slow = fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next # Step 2: Reverse second half second = slow.next slow.next = None prev = None while second: second.next, prev, second = prev, second, second.next second = prev # Step 3: Merge alternating first = head while second: tmp1, tmp2 = first.next, second.next first.next = second second.next = tmp1 first, second = tmp1, tmp2
06
Section Six ยท Analysis

Complexity Analysis

ApproachTimeSpaceTrade-off
Array + RebuildO(n)O(n)Simple but extra memory
Three-step โ† optimal O(n) O(1) Find middle + reverse + merge
07
Section Seven ยท Edge Cases

Edge Cases & Pitfalls

CaseInputWhy It Matters
Single/two nodes1 or 1โ†’2Already in correct order โ€” nothing to do.
Even length1โ†’2โ†’3โ†’4Split: [1,2] and [4,3]. Merge: 1โ†’4โ†’2โ†’3.
Odd length1โ†’2โ†’3โ†’4โ†’5First half has extra node. Merge leaves middle node (3) at end.
โš  Common Mistake: Not cutting the first half (forgetting slow.next = null). Without the cut, the merge step creates a cycle โ€” nodes from the first half still point forward into the reversed second half.

โ† Back to Linked List problems