Detect and Remove Loop
Posted: 10 Dec, 2019
Difficulty: Moderate
Given a singly linked list, you have to detect the loop and remove the loop from the linked list, if present. You have to make changes in the given linked list itself and return the updated linked list.
Expected Complexity: Try doing it in O(n) time complexity and O(1) space complexity. Here, n is the number of nodes in the linked list.
Input format:
The first line of input contains two values: the number of nodes in the linked list and the value of the kth node from which the last node connects to form the loop while the second line of input contains the given linked list.
The value of k should be greater than or equal to 0 and less than equal to n. For, k equal to 0, there is no loop present in the linked list and for k equal to n, the last node is connected to itself to form the cycle.
Output Format:
The only output line contains the linked list after removing the loop if present.
Constraints:
1 <= N <= 100000.
1 <= ‘VAL’ <= 1000 .
Time limit: 1 sec
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 the head and maintain a prev node to store the previous node of the current outer loop node.
- If the inner-loop visits the node next to the outer-loop node, then a cycle exist,we will simply set the prev node's next as NULL that will break the cycle and return the linked list else repeat the process for the next iteration of outer-loop.
- If outer-loop reaches the end of list or null, then no cycle exist, return head.
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.
- Declare ‘PREV’ that will store the previous node of the current node.
- Check if the current node is present in the loop up table, if present then there is a cycle and will set the next value of ‘PREV’ node as NULL that will break the cycle and return the linked list otherwise, put the node reference into the lookup table update the ‘PREV’ node 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 and break this loop otherwise repeat the process
- If fast reaches the end or null then the execution stops and we can conclude that no cycle exists and return head pointer.
- Now,to find the starting point of the cycle set fast at head and slow at current slow and move both the pointers by one step.The pointer where they meet is the starting point of the cycle.Set this node as ‘START’.
- Iterate the list to find the previous node of ‘START’ and set the next of that previous node as an empty node.
SIMILAR PROBLEMS
Insertion In Circular Linked List
Posted: 21 Apr, 2022
Difficulty: Easy
Insertion In A Singly Linked List
Posted: 22 Apr, 2022
Difficulty: Easy
Remove Loop
Posted: 22 Apr, 2022
Difficulty: Moderate
Deletion In Doubly Linked List
Posted: 22 Apr, 2022
Difficulty: Easy
Insertion In Doubly Linked List
Posted: 24 Apr, 2022
Difficulty: Easy