Problem Walkthrough

Reorder List LeetCode 143 — Three Steps

Decompose LeetCode 143 into three clean subproblems: find the middle with slow/fast pointers, reverse the second half in place, then merge the two halves alternating — each step a classic linked list primitive.

9 min read|

Reorder List

LeetCode 143

Problem Overview

LeetCode 143 — Reorder List — gives you the head of a singly linked list L0→L1→...→Ln-1→Ln and asks you to reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→... You must do this in-place — no allocating a new list or copying values between nodes. Only node rewiring is allowed.

The resulting list interleaves nodes from the front and back of the original, alternating one from the start then one from the end. For a five-node list 1→2→3→4→5, the answer is 1→5→2→4→3. For a four-node list 1→2→3→4, the answer is 1→4→2→3. The middle node (or the first of two middles for even-length lists) always ends up last.

What makes this problem elegant is that it can be decomposed entirely into three simpler problems you may already know. No new algorithms are required — just the right composition of existing linked list primitives applied in sequence.

  • Constraints: list length is between 1 and 50,000 nodes
  • Node values are in the range [1, 1000]
  • You must modify the list in-place — do not change node values, only relink pointers
  • The function returns void; the input list is mutated directly

The Three-Step Strategy

The key insight for LeetCode 143 is to stop thinking of it as one problem and start thinking of it as three. Step 1: find the middle of the linked list using the classic slow/fast pointer technique. Step 2: reverse the second half of the list (from middle+1 to the end). Step 3: merge the two halves by interleaving nodes from the first half and the reversed second half.

Each of these three steps corresponds to a well-known LeetCode problem in its own right: finding the middle is LeetCode 876, reversing a linked list is LeetCode 206, and merging two lists alternately is a pattern from LeetCode 21. If you have solved any of those, you already have the building blocks.

The beauty of this decomposition is that each step is independently verifiable. You can write and test each helper function on its own before combining them. This modular approach also means the code stays readable even in an interview — three focused functions rather than one tangled loop.

💡

Decompose to Conquer

This problem combines three fundamental linked list techniques: find middle (LeetCode 876), reverse linked list (LeetCode 206), and merge alternating. Mastering each individually makes the combination straightforward — and gives you three separate interview problems for the price of one.

Step 1: Find the Middle

Use the slow/fast pointer technique to find the middle of the list. Initialize both slow and fast at the head. On each step, move slow one node forward and fast two nodes forward. When fast reaches the end of the list (fast is null or fast.next is null), slow is sitting at the middle node.

For an odd-length list like 1→2→3→4→5, slow lands on node 3 — the true middle. For an even-length list like 1→2→3→4, slow lands on node 2 — the first of the two middle nodes. In both cases, the second half of the list starts at slow.next.

After finding the middle, you split the list: save the head of the second half as slow.next, then set slow.next = null to sever the first half from the second. This disconnection is critical — without it, the merge step in Step 3 will create a cycle in the list.

  1. 1Initialize slow = head, fast = head
  2. 2While fast != null and fast.next != null: slow = slow.next, fast = fast.next.next
  3. 3Save secondHalf = slow.next
  4. 4Disconnect: set slow.next = null to end the first half
  5. 5Now firstHalf starts at head, secondHalf starts at the saved pointer

Step 2: Reverse the Second Half

Apply the standard iterative linked list reversal to the second half. Initialize prev = null and cur = secondHalf. On each iteration: save next = cur.next, then set cur.next = prev, then advance prev = cur and cur = next. Repeat until cur is null. At the end, prev is the new head of the reversed second half.

For the list 1→2→3→4→5 with second half 4→5, this produces 5→4. For the four-node list 1→2→3→4 with second half 3→4, this produces 4→3. After reversal, the last node of the reversed second half will have a null next pointer — which is exactly what you want for clean merging.

This is literally the solution to LeetCode 206 Reverse Linked List applied to a sublist. If you have that problem memorized, Step 2 takes seconds to write. The three-line iterative reversal is one of the most frequently recurring patterns in linked list interviews.

ℹ️

LeetCode 206 in Disguise

Step 2 is exactly LeetCode 206 Reverse Linked List applied to the second half. The iterative reversal — prev=null, cur=head2; while cur: next=cur.next, cur.next=prev, prev=cur, cur=next; head2=prev — is a pattern worth having memorized cold. If you know LeetCode 206, this step is free.

Step 3: Merge Alternating

With the first half and the reversed second half in hand, merge them by interleaving: take one node from the first half, then one node from the reversed second half, and repeat until the second half is exhausted. The first half may have one more node than the second (the original middle node for odd-length lists), which becomes the tail naturally.

Use two pointers, first and second, starting at the heads of the two halves. On each iteration: save first.next and second.next, then set first.next = second, then set second.next = saved first.next, then advance both pointers to their saved nexts. Repeat while second is not null.

For 1→2→3 and 5→4: iteration 1 produces 1→5→2→3 (first advances to 2, second advances to 4); iteration 2 produces 1→5→2→4→3 (first advances to 3, second is now null). The loop ends and the list is correctly reordered.

  1. 1Initialize first = head1, second = head2
  2. 2While second != null: save tmp1 = first.next, tmp2 = second.next
  3. 3Set first.next = second, second.next = tmp1
  4. 4Advance first = tmp1, second = tmp2
  5. 5Loop ends when second is null; first half tail is already correct

Code Walkthrough: Python and Java

In Python, the three steps map to three helper-like blocks: find middle with slow/fast, reverse second half iteratively, then merge alternating. The full solution runs in O(n) time and O(1) space — no stack, no array, no extra nodes beyond the original list structure.

In Java the logic is identical: ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } — then reverse from slow.next with the three-variable swap loop, then interleave. The structure is clean and modular whether written as one function or three helpers.

Across both languages the total code is under 30 lines. Splitting into three named helper methods (findMiddle, reverseList, mergeAlternating) makes it exceptionally readable and testable. Each function can be unit-tested in isolation, which is a strong signal of clean design in a systems-thinking interview.

⚠️

Disconnect Before Merge

After finding the middle, you MUST set slow.next = null to disconnect the first half from the second. If you skip this, the first half still points into the second half. When you later call reverse on the second half, you will corrupt the first half — and the merge step will create an infinite cycle. Disconnect first, always.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now