Skip to main content
22

Reorder List

Mediumlinked listfast & slow pointers

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

This problem uses:Linked ListRead the beginner guide

You're given the head of a linked list L0 → L1 → … → Ln-1 → Ln. Reorder it in place to:

text
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

Understand the problem

In words: take the first node, then the last, then the second, then the second-to-last, and so on, alternating between the front and the back until you meet in the middle.

  • In place means you rewire the existing nodes' next pointers. You can't just change the values inside the nodes.
  • The function returns nothing. The tests read the list starting from head after your function runs.

Example 1

text
Input:  head = [1, 2, 3, 4]
Result: [1, 4, 2, 3]

First 1, last 4, second 2, second-to-last 3.

Example 2

text
Input:  head = [1, 2, 3, 4, 5]
Result: [1, 5, 2, 4, 3]

1, 5, 2, 4, and the middle 3 ends up last.

Example 3

text
Input:  head = [1]
Result: [1]

Constraints

  • 1 <= number of nodes <= 5 * 10^4

Hints

Stuck? Open one hint at a time. Each one gives away a little more than the last.

    The idea in one sentence

    Cut the list in half, reverse the second half, then weave the two halves together one node at a time.

    The simple approach (and its cost)

    Put every node into an array. Now you can reach any node by index, so relink them using two indices, one from the front and one from the back: arr[0] → arr[n-1] → arr[1] → arr[n-2] → ….

    This is easy to get right and a fine first answer. Its cost is O(n) extra memory for the array. With a few linked list tricks you can do it in O(1).

    The key insight

    Look closely at the answer:

    text
    1 → 2 → 3 → 4 → 5
    result: 1 → 5 → 2 → 4 → 3

    The nodes 1, 2, 3 (first half) appear in order, and the nodes 5, 4 (second half) appear reversed. So the problem is really three smaller problems you may already know:

    1. Find the middle with fast and slow pointers (from Linked List Cycle).
    2. Reverse the second half (from Reverse Linked List).
    3. Weave the two halves together (similar to Merge Two Sorted Lists, but alternating instead of comparing).

    Step by step

    1 → 2 → 3 → 4 → 5:

    1. Find the middle. slow moves 1 step, fast moves 2, until fast can't take two more steps:

    slowfast
    11
    23
    35

    slow stops at 3. Cut after it: first half 1 → 2 → 3, second half 4 → 5.

    2. Reverse the second half: 4 → 5 becomes 5 → 4.

    3. Weave, taking one node from each half in turn:

    firstsecondAfter linking
    151 → 5 → 2 …
    241 → 5 → 2 → 4 → 3
    3(empty)stop

    Result: 1 → 5 → 2 → 4 → 3 ✅

    When the length is odd, the first half gets the extra (middle) node, so it's never shorter than the second. That's why the weaving loop can simply stop when the second half runs out.

    The code

    The reference solution is below, with comments on every important line.

    How fast is it?

    • Time: O(n). Finding the middle, reversing and weaving are each one pass.
    • Memory: O(1). Only a handful of pointers.

    Common mistakes

    • Forgetting to cut the list (slow.next = null). Then the first half still links into the second, and the weave creates a loop.
    • Losing nodes while weaving. Save first.next and second.next before you change either.
    • Returning the head. The function returns nothing: the tests look at head, which you rearranged in place.

    Going deeper

    This problem is a great example of composing small techniques. In an interview, saying "this is find-middle + reverse + merge" out loud before coding shows you see the structure, and lets you write (and test) each piece separately.

    Reference solution

    js
    /**
     * @param {ListNode} head
     * @return {void}
     */
    export default function reorderList(head) {
      if (!head) return
    
      // 1. Find the middle: slow moves 1 step, fast moves 2. When fast can't go on, slow is the middle.
      let slow = head
      let fast = head
      while (fast.next && fast.next.next) {
        slow = slow.next
        fast = fast.next.next
      }
    
      // 2. Cut after the middle, then reverse the second half (same as Reverse Linked List).
      let second = slow.next
      slow.next = null // the first half now ends at the middle
      let prev = null
      while (second) {
        const next = second.next
        second.next = prev
        prev = second
        second = next
      }
    
      // 3. Weave: one node from the first half, one from the reversed second half, repeat.
      let first = head
      second = prev
      while (second) {
        // Save both "next" pointers before relinking anything.
        const firstNext = first.next
        const secondNext = second.next
        first.next = second
        second.next = firstNext
        first = firstNext
        second = secondNext
      }
    }
    ts
    export default function reorderList(head: ListNode | null): void {
      if (!head) return
    
      // 1. Find the middle: slow moves 1 step, fast moves 2. When fast can't go on, slow is the middle.
      let slow = head
      let fast = head
      while (fast.next && fast.next.next) {
        slow = slow.next!
        fast = fast.next.next
      }
    
      // 2. Cut after the middle, then reverse the second half (same as Reverse Linked List).
      let second: ListNode | null = slow.next
      slow.next = null // the first half now ends at the middle
      let prev: ListNode | null = null
      while (second) {
        const next: ListNode | null = second.next
        second.next = prev
        prev = second
        second = next
      }
    
      // 3. Weave: one node from the first half, one from the reversed second half, repeat.
      let first: ListNode | null = head
      second = prev
      while (first && second) {
        // Save both "next" pointers before relinking anything.
        const firstNext: ListNode | null = first.next
        const secondNext: ListNode | null = second.next
        first.next = second
        second.next = firstNext
        first = firstNext
        second = secondNext
      }
    }
    python
    def reorder_list(head: ListNode | None) -> None:
        if not head:
            return
    
        # 1. Find the middle: slow moves 1 step, fast moves 2. When fast can't go on, slow is the middle.
        slow = fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
    
        # 2. Cut after the middle, then reverse the second half (same as Reverse Linked List).
        second = slow.next
        slow.next = None  # the first half now ends at the middle
        prev = None
        while second:
            # Flip second's arrow and step forward, all in one assignment.
            second.next, prev, second = prev, second, second.next
    
        # 3. Weave: one node from the first half, one from the reversed second half, repeat.
        first, second = head, prev
        while second:
            # Save both "next" pointers before relinking anything.
            first_next, second_next = first.next, second.next
            first.next = second
            second.next = first_next
            first, second = first_next, second_next