Apex › Storage & databases

Cycle detection — Floyd's tortoise & hare

Find a loop in a chain using O(1) memory. Two pointers walk a ρ-shaped graph — one slow, one twice as fast — until the hare laps the tortoise inside the loop; a second phase then finds exactly where the loop begins. Step through both phases and watch the pointers meet, with the same trick that powers Pollard's rho factorization.

More in Storage & databases