1. Both at head
Floyd's tortoise and hare start together.
let slow = head;
let fast = headTyporithm · easy
Step 1 of 8 · intro card
Linked Lists
Floyd's tortoise and hare: if a fast pointer ever meets the slow one, the list has a cycle.
A cycle means some node's next eventually points back into the list.
Moving fast twice as quickly, the gap to slow shrinks by one each step, so they meet inside any loop.
If fast reaches null, the list ends and there is no cycle.
Two speeds from the head.
Meeting means a cycle.
fast hitting null means none.
function hasCycle(head) {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
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.
Floyd's tortoise and hare start together.
let slow = head;
let fast = headfast advances two nodes per slow's one.
fast = fast.next.nextInside a loop, fast laps slow and they coincide.
if (slow === fast)If fast reaches null, the list terminates with no loop.
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.