Using Floyd's Tortoise and Hare algorithm to detect cycles in a linked list.
A cycle in a linked list occurs when a node's `next` pointer points back to a previous node in the list, creating an infinite loop. Detecting such a cycle is a classic problem. The most elegant and efficient solution is Floyd's Cycle-Finding Algorithm, also known as the 'Tortoise and the Hare' algorithm. It uses two pointers, a 'slow' pointer and a 'fast' pointer, both initialized to the head of the list. In each iteration, the slow pointer moves one step forward, while the fast pointer moves two steps forward. If the list is linear (has no cycle), the fast pointer will reach the end (null) first. However, if there is a cycle, the fast pointer will eventually enter the cycle and, at some point, it will lap the slow pointer (which will also have entered the cycle). Therefore, if the slow and fast pointers ever meet at the same node, we can conclude that a cycle exists. This algorithm works in O(n) time, where `n` is the number of nodes in the list, and it uses O(1) extra space. Once a cycle is detected, the algorithm can be extended to find the starting node of the cycle as well.