Solution: Linked List Cycle
Let’s solve the Linked List Cycle problem using the Fast and Slow Pointers pattern.
Statement#
Check whether or not a linked list contains a cycle. If a cycle exists, return TRUE. Otherwise, return FALSE. The cycle means that at least one node can be reached again by traversing the next pointer.
Constraints:
Let n be the number of nodes in a linked list.
-
n -
Node.data
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow, based on considerations such as time complexity and any implementation constraints.
Naive approach#
The naive approach is to traverse the linked list and store the current node in a hash set. At each iteration, we check if the current node is already present in the hash set. If it is, we’ve found a cycle and return TRUE. Otherwise, we add the node to the hash set. If the traversal has been completed, return FALSE, since we’ve reached the end of the linked list without encountering a cycle.
The time complexity is , since we only traverse the linked list once, where represents the total number of nodes in a linked list. The space complexity of the naive approach is also , since in the worst case, we store nodes in the hash set.
Optimized approach using fast and slow pointers#
This problem can be solved efficiently using the fast and slow pointers technique, where the fast pointer moves two steps, and the slow pointer moves one step in the linked list.
If there is no cycle in the linked list, the fast pointer will reach the end of the linked list before the slow pointer. If there is a cycle, the fast pointer will eventually catch up to the slow pointer, since it is moving faster. When this happens, we know that we have a cycle in the linked list. The following steps can be performed to implement the algorithm above:
-
We initialize two pointers,
fastandslow, which point to the head of the linked list. -
We traverse the linked list using these two pointers. They move in the following way:
- The
slowpointer moves only one node forward in each iteration. - The
fastpointer moves two nodes forward in each iteration, which means that it skips a node.
- The
-
If the
fastpointer becomes NULL, we have reached the end of the linked list. Since no cycle exists, return FALSE. -
If both
slowandfastpointers become equal to each other, return TRUE, since a cycle exists in the linked list.
Here’s a demonstration of the algorithm above:
1 of 6
2 of 6
3 of 6
4 of 6
5 of 6
6 of 6
Here's the coded solution:
Solution summary#
To recap, the solution to this problem can be divided into the following three parts:
- Initialize both the slow and fast pointers to the head node.
- Move both pointers at different rates, i.e. the slow pointer will move one step ahead whereas the fast pointer will move two steps.
- If both pointers are equal at some point, we know that a cycle exists.
Time complexity#
The time complexity of the algorithm is , where is the number of nodes in the linked list.
Space complexity#
The space complexity of the algorithm above is .
Linked List Cycle
Middle of the Linked List