Tortoise & Hare algorithm is also known as Floyd's Cycle-Finding Algorithm.This is a technique for detecting infinite loops inside a linked list, symlinks, state machines etc.
To detect a loop inside a linked list
Source
Python code to understand Tortoise and Hare algorithms
To detect a loop inside a linked list
Source
- Make a pointer to start of the linked list and call it Hare
- Make a pointer to the head of the linked list and call it Tortoise
- advance the Hare by two and the tortoise by one for every interation.
- There's a loop if the Tortoise and Hare meet
- There's no loop if the Hare reaches the end.
Algorithm is intersting as it takes O(n) time and O(1) space.
Python code to understand Tortoise and Hare algorithms
Source
With this Algorithm we detect the loops and iterations in a circularly linked list as suggested by the name Floyd's Cycle-finding Algorithm.
More Reference: Coding Freak