1. Find first middle
Stop slow at the end of the first half.
while (fast.next !== null && fast.next.next !== null)Typorithm · medium
Step 1 of 8 · intro card
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.
Split at the first middle.
Reverse the second half.
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;
}
}Connect each part of the code to the concept before you type.
Stop slow at the end of the first half.
while (fast.next !== null && fast.next.next !== null)Sever the list into two halves.
slow.next = nullFlip the second half so it runs back to front.
second.next = prevInterleave nodes from the front and reversed back.
first.next = secondRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.