The first line of input contains a single integer T, representing the number of test cases or queries to be run.
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 the tail connects to. If "pos" is -1, then there is no cycle in the linked list.
For each test case, print two lines.
The first line contains 'True' if the linked list has a cycle, otherwise 'False'.
The second line contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
You don't have to explicitly print anything yourself. It has been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5 * 10^4
-1 <= pos < N
-10^9 <= data <= 10^9 and data != -1
Where 'N' is the size of the singly linked list, and "data" is the Integer data of the singly linked list.
The basic idea is that for every node we will check if there is any node to its left that is creating a cycle.
Algorithm
In this approach, we will use a hash table to store the visited nodes. We will traverse the list, and for each node, if it is not present in the hash table we will add it, else if the node is already there is the hash table, it means there is a cycle. Assume this node as ‘cur’, to remove the cycle we have to find the previous node of the ‘cur’ node and change its ‘next’ values to NULL.
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 the fast pointer will reach the end of the linked list well before the slow pointer reaches the end or NULL.
Why slow and fast meet at a node?
After some moves, there will be a situation where both fast and slow pointers are in the cycle.
After each move, the distance between pointer fast and slow decreases by 1. So after 2 moves (2 is the initial distance between fast and slow pointer), both pointers will meet a node.
Removing the cycle
We can find the cycle as we did in approach 2
We have to find ‘start’.
To remove the cycle we have to find the previous node of ‘start’ and change its ‘next’ value to NULL.
This works because the distance of head node from ‘start’ is equal to the distance of ‘converge’ node from start.
Explanation
Let ‘k’ denote the length of the cycle.
We will prove a=b.
Distance travelled by fast pointer = 2 * (Distance travelled by slow pointer)
a + x*k + (k-b) = 2*( a + y*k + (k-b) )
Where x is complete cyclic rounds made by fast pointer and y is complete cyclic rounds made by slow pointer before they meet
=> x*k = a + 2*y*k + k - b
=> a - b = (x - 2*y - 1)*k
When we move in the cycle, the actual distance (displacement) we cover from the ‘start’ node is D%k ( where ‘D’ is the total distance we moved in the cycle )
(x - 2*y - 1)*k is a multiple,
=> (x-2*y-1)*k % k=0
Hence, we can say a=b.
Deletion In Doubly Linked List
Deletion In Doubly Linked List
Deletion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
LRU Cache
Delete Nodes On Regular Intervals
Add One To Linked List