Reorder List

MediumLinked List · Fast & Slow · Reverse · Merge🧩 Linked List TechniquesLeetCode #143 ↗Updated

Problem

Given the head of a singly linked list L0 → L1 → … → Ln, rearrange its nodes in place into L0 → Ln → L1 → Ln-1 → L2 → … (first, last, second, second-last, …). Only links may change, not node values. Nothing is returned.
Input[1,2,3,4]Output[1,4,2,3]
Input[1,2,3,4,5]Output[1,5,2,4,3]

1 ≤ n ≤ 5 × 104  ·  1 ≤ Node.val ≤ 1000

Try it yourself

Java
class Solution {
    public void reorderList(ListNode head) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
The result is just the first half zipped with the second half read backwards. Split at the middle (fast/slow), reverse the second half in place, then alternate nodes from the two halves.
PatternLinked List Techniques

Instinct Nodes joined by next pointers and the task is to reverse, splice, merge, reorder or remove in place - grab a dummy head and rewire pointers carefully.

Memory hack Save next, flip, step - never let go of a rope before you've grabbed the next one.

Open the Linked List Techniques guide →

Approaches

Brute ForceRepeatedly Detach the Tail

Time O(n²)Space O(1)

Standing at cur, walk to the node before the tail, unhook the tail and splice it right after cur; then jump cur two steps (past the spliced node). Stop once fewer than three nodes remain from cur. Finding the tail every time is what makes it quadratic.

Java
public void reorderList(ListNode head) {
    ListNode cur = head;
    while (cur != null && cur.next != null && cur.next.next != null) {
        ListNode beforeTail = cur;
        while (beforeTail.next.next != null) beforeTail = beforeTail.next;
        ListNode tail = beforeTail.next;
        beforeTail.next = null;   // detach the tail
        tail.next = cur.next;     // splice it right after cur
        cur.next = tail;
        cur = tail.next;
    }
}

No extra memory, but each step re-walks the list — too slow for 5×104.

At a glance

LevelApproachTimeSpace
Brute ForceRepeatedly Detach the TailO(n²)O(1)
BetterArray of Nodes + Two PointersO(n)O(n)
OptimalFind Middle, Reverse Second Half, MergeO(n)O(1)
Common pitfalls
  • Not cutting slow.next = null — the halves stay connected and form a cycle.
  • Swapping values instead of relinking nodes — violates the statement.
  • Losing first.next / second.next before rewiring during the merge.
Interview tips
  • Point out the first half is never shorter, so loop on the reversed half.
  • Mention that this one problem combines three patterns: middle, reverse, merge.
  • Verify with both even [1,2,3,4] and odd [1,2,3,4,5] lengths.
Pattern
Middle + reverse + weave → any reordering that pairs the i-th node from the front with the i-th from the back.
My notes saved in this browser