# Floyd Cycle Detection Algorithm to detect the cycle in a linear Data Structure

Data StructureAnalysis of AlgorithmsAlgorithms

Floyd Cycle is one of the cycle detection algorithms to detect the cycle in a given singly linked list.

In the Floyd Cycle algorithm, we have two pointers that initially point at the head. In Hare and Tortoise’s story, Hare moves twice as fast as Tortoise, and whenever the hare reaches the end of the path, the tortoise reaches the middle of the path.

## Algorithm

• Initialize Hare and Tortoise at the head node of the List.

• Initially, the hare moves twice as fast as the tortoise.

• Move the hare and tortoise both and find if the hare reaches the end of the Linked List, return as there is no loop in the list.

• Otherwise, both Hare and Tortoise will go forward.

• If Hare and Tortoise are at the same Node, then return since we have found the list cycle.

## Pseudocode for the above algorithm

tortoise := headNode
return 'Cycle Detected'