AlgoViz

Fast and slow pointers

medium 20 min

Two pointers walk the same chain, one node at a time and two nodes at a time.

Where it shows up

The same pattern appears across everyday software.

  • Memory and reference checks

    Garbage collectors, serialisers and debuggers walking chains of references can loop forever on a cyclic structure. Floyd's check spots the loop with no extra memory, which matters when the structure is huge.

  • Pollard's rho factorisation

    Pollard's rho algorithm iterates x → x² + c (mod n) and uses Floyd's tortoise and hare to find a repeat modulo a hidden factor of n. The algorithm is named after the same rho shape.

  • Testing random number generators

    Every pseudo-random generator with a finite state eventually repeats. Cycle detection measures where the repeat begins and the period length using constant memory, without storing the whole output.

  • Detecting loops in state machines and simulations

    Any deterministic step function (a game of life board, a hash chain, a sequence of configurations) can be checked for a loop the same way: one copy steps once, the other twice.

  • Splitting lists in half

    Merge sort on linked lists, reorder-list and palindrome checks all start by finding the middle in one pass with slow and fast pointers.