1. Two speeds
Both start at the head; one moves twice as fast.
let slow = head;
let fast = headTyporithm · easy
Step 1 of 8 · intro card
Linked Lists
Run a slow and a fast pointer; when fast reaches the end, slow is at the middle.
The naive way counts the length then walks halfway — two passes.
A fast pointer moving two steps for every one of slow lands slow at the middle in a single pass.
For even lengths this returns the second of the two middle nodes.
Fast moves two, slow moves one.
Stop when fast hits the end.
One pass, no length count.
function middleNode(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}Connect each part of the code to the concept before you type.
Both start at the head; one moves twice as fast.
let slow = head;
let fast = headfast moves two steps, so both fast and fast.next must exist.
while (fast !== null && fast.next !== null)When fast reaches the end, slow sits at the middle.
fast = fast.next.nextslow covers half the distance fast covers.
return slowRead 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.