This article explains how to detect a cycle in a linked list using two popular approaches: the HashSet method and Floyd's Cycle Detection Algorithm (also known as the Fast Slow Pointer technique).
In the HashSet approach, we traverse the linked list and store each visited node in a set. If we encounter a node that is already in the set, a cycle exists. This method uses O(n) extra space.
Floyd's algorithm uses two pointers moving at different speeds: a slow pointer that moves one step at a time and a fast pointer that moves two steps. If there is a cycle, the two pointers will eventually meet. This algorithm detects cycles in O(n) time with O(1) space.
For a hands-on demonstration, watch the video tutorial linked above. The video covers implementation in Java, Python, and C++.