Typorithm · medium

Reorder List

reorder-listlinked-listssplitreverse-halfweave
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Reorder List

0%

Linked Lists

Split at the middle, reverse the second half, then weave the two halves alternately.

Reorder turns 1→2→3→4 into 1→4→2→3 by alternating front and back nodes.

Find the middle, cut the list, and reverse the second half so its end is reachable first.

Then weave one node from each half at a time until the back half is consumed.

01

Split at the first middle.

02

Reverse the second half.

03

Weave front and back alternately.

function reorderList(head) {
  let slow = head;
  let fast = head;

  while (fast.next !== null && fast.next.next !== null) {
    slow = slow.next;
    fast = fast.next.next;
  }

  let second = slow.next;
  slow.next = null;
  let prev = null;

  while (second !== null) {
    const next = second.next;
    second.next = prev;
    prev = second;
    second = next;
  }

  let first = head;
  second = prev;

  while (second !== null) {
    const t1 = first.next;
    const t2 = second.next;
    first.next = second;
    second.next = t1;
    first = t1;
    second = t2;
  }
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Find first middle

Stop slow at the end of the first half.

while (fast.next !== null && fast.next.next !== null)

2. Cut in two

Sever the list into two halves.

slow.next = null

3. Reverse second half

Flip the second half so it runs back to front.

second.next = prev

4. Weave together

Interleave nodes from the front and reversed back.

first.next = second

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Reorder List: Lesson & Practice · Typorithm | ApplyDjinn