Reorder List

Difficulty: Medium

Given the head of a singly linked list L0, L1, L2, ..., Ln, rearrange it in place to read L0, Ln, L1, Ln-1, L2, Ln-2, and so on — alternating one node from the front and one from the back, working inward.

Change the pointers between existing nodes; don't copy or swap the values.

Examples

Input: head = [1, 2, 3, 4]
Output: [1, 4, 2, 3]

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

Input: head = [1]
Output: [1]

A single node has nothing to interleave with.

Constraints

  • The number of nodes is between 1 and 5 * 10^4.

  • 1 <= Node.val <= 1000

Approach

The direct approach puts every node in an array for random access, then uses one index from the front and one from the back, re-linking the list by alternating between them.

The memory-efficient approach skips the array:

  • Find the middle with fast/slow pointers.
  • Reverse the second half in place, so its nodes come out back to front.
  • Zip the first half and the reversed second half together, one node at a time.

Solutions

Brute Force — Random Access via Array

Put every node in an array, in order. With one index at the front and one at the back, rewire next pointers to alternate between them, moving the indices inward until they meet.

class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function reorderList(head) {
  if (!head) return;

  const nodes = [];
  let curr = head;
  while (curr) {
    nodes.push(curr);
    curr = curr.next;
  }

  let i = 0;
  let j = nodes.length - 1;

  while (i < j) {
    nodes[i].next = nodes[j];
    i++;
    if (i === j) break;
    nodes[j].next = nodes[i];
    j--;
  }

  nodes[i].next = null; // whichever node ends up last must terminate the list
}

Time: O(n) · Space: O(n)

Optimal — Split, Reverse Second Half, Merge

Find the middle with a slow pointer (one step) and a fast pointer (two steps): when fast runs out of room, slow is at the middle. Cut the list there.

Reverse the second half in place, so it now runs back to front.

Then weave the halves together: one node from the first half, one from the reversed second half, and repeat, updating next as you go.

class ListNode {
  constructor(val, next = null) {
    this.val = val;
    this.next = next;
  }
}

function reorderList(head) {
  if (!head || !head.next) return;

  // 1. Find the middle.
  let slow = head;
  let fast = head;
  while (fast.next && fast.next.next) {
    slow = slow.next;
    fast = fast.next.next;
  }

  // 2. Split and reverse the second half.
  let second = slow.next;
  slow.next = null;
  let prev = null;
  while (second) {
    const next = second.next;
    second.next = prev;
    prev = second;
    second = next;
  }

  // 3. Merge the first half with the reversed second half.
  let first = head;
  second = prev;
  while (second) {
    const firstNext = first.next;
    const secondNext = second.next;
    first.next = second;
    second.next = firstNext;
    first = firstNext;
    second = secondNext;
  }
}

Time: O(n) · Space: O(1)