Cycle Detection in a Singly Linked List
Posted: 10 Dec, 2019
Difficulty: Moderate
You have given a Singly Linked List of integers, determine if it forms a cycle or not.
A cycle occurs when a node's next points back to a previous node in the list. The linked list is no longer linear with a beginning and end—instead, it cycles through a loop of nodes.
Note: Since, it is binary problem, there is no partial marking. Marks will only be awarded if you get all the test cases correct.
Input format :
The first line of each test case contains the elements of the singly linked list separated by a single space and terminated by -1 and hence -1 would never be a list element.
The second line contains the integer position "pos" which represents the position (0-indexed) in the linked list where tail connects to. If "pos" is -1, then there is no cycle in the linked list.
Output format :
The only line of output contains 'true' if linked list has a cycle or 'false' otherwise.
You don't have to explicitly print by yourself. It has been taken care of.
Constraints :
0 <= N <= 10^6
-1 <= pos < N
-10^9 <= data <= 10^9 and data != -1
Where 'N' is the size of the singly linked list, "pos" represents the position (0-indexed) in the linked list where tail connects to and "data" is the Integer data of singly linked list.
Time Limit: 1 sec
Note :
Try to solve this problem in O(N) Time Complexity and O(1) space Complexity
Approach 1
We are going to have two loops outer-loop and inner-loop
- Maintain a count of the number of nodes visited in outer-loop.
- For every node of the outer-loop, start the inner loop from head.
- If the inner-loop visits the node next to the outer-loop node, then return true, else repeat the process for the next iteration of outer-loop.
- If outer-loop reaches the end of list or null, then return false.
Approach 2
We are going to maintain a lookup table(a Hashmap) that basically tells if we have already visited a node or not, during the course of traversal.
- Visit every node one by one, until null is not reached
- Check if the current node is present in the loop up table, if present then there is a cycle and will return true, otherwise, put the node reference into the lookup table and repeat the process.
- If we reach at the end of the list or null, then we can come out of the process or loop and finally, we can say the loop doesn't exist.
Approach 3
- The idea is to have 2 pointers: slow and fast. Slow pointer takes a single jump and corresponding to every jump slow pointer takes, fast pointer takes 2 jumps. If there exists a cycle, both slow and fast pointers will reach the exact same node. If there is no cycle in the given linked list, then fast pointer will reach the end of the linked list well before the slow pointer reaches the end or null.
- Initialize slow and fast at the beginning.
- Start moving slow to every next node and moving fast 2 jumps, while making sure that fast and its next is not null.
- If after adjusting slow and fast, if they are referring to the same node, there is a cycle otherwise repeat the process
- If fast reaches the end or null then the execution stops and we can conclude that no cycle exists.
SIMILAR PROBLEMS
Ninja And The List
Posted: 19 Jan, 2022
Difficulty: Moderate
Insertion In Circular Linked List
Posted: 21 Apr, 2022
Difficulty: Easy
Insertion In A Singly Linked List
Posted: 22 Apr, 2022
Difficulty: Easy
Deletion In Doubly Linked List
Posted: 22 Apr, 2022
Difficulty: Easy
Insertion In Doubly Linked List
Posted: 24 Apr, 2022
Difficulty: Easy