please tell me how to create such a linked list and implement the removal of cycle
About the implementation of this
Hi @aslesha.j7
Floyd cycle detection method is used by keeping taking 2 pointers fast and slow. Slow moves by 1 node everytime and fast moves by 2 nodes each time. If fast node reaches null node then no cycle exist else when slow == fast then we detect a cycle. For removing the cycle, first calculate the length of the cycle, then take 2 pointers. Advance first pointer by k nodes and second pointer stays at head. Then advance these two nodes 1 by 1 and when the both are equal again then it means we have found out the start of the cycle. So, mark next of previous node from start of cycle as null. Hence it removes the cycle. You can refer to course video for graphic implementation.
Hope it Helps.
can you tell me what is k here?
and how to make a linked list which contains a cycle in between?
k is the length of the cycle that exists in the linked list.
To create such a list , decide which node in the list should be the start of the cycle, then make end node’s next as this particular node.
Hope it Helps.
Im not able to create such a linked list. Can you please help
Let’s suppose originally we make a simple linked list as : 1->2->3->4->5. Now the node on which you want to make a cycle, store its address in dummy node. Now make 5->next = dummy_node. Hence the cycle is created.
Hope it Helps.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.