1. Two speeds
Both runners start at the head; speed difference does the work.
let slow = head;
let fast = head;Typorithm · medium
Step 1 of 8 · intro card
Basic Patterns
Two pointers at different speeds meet only if the structure loops.
A slow pointer moves one step per turn while a fast pointer moves two.
If the list ends, fast hits null and the answer is no cycle.
If the list loops, fast eventually laps slow and they point at the same node.
Same start, different speeds.
Guard both fast and fast.next.
Identity match means a cycle.
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
return true;
}
}
return false;
}Connect each part of the code to the concept before you type.
Both runners start at the head; speed difference does the work.
let slow = head;
let fast = head;fast moves two steps, so both fast and fast.next must exist.
while (fast && fast.next)slow advances one node while fast advances two.
slow = slow.next;
fast = fast.next.next;In a cycle the fast runner laps the slow one and they share a node.
if (slow === fast)If fast reaches the end, the list terminates and has no cycle.
return falseRead 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.