I know algo and input fn of ll , But how to create cycle in ll while taking input? I can take input of singly ll but input with cycle is different.
How to take input?
Say If INPUT IS 1 2 3 4 5 2 3 -1
form a linked list 1->2->3->4>5 then you see that 2 has already been visited , so connect the next pointer of 5 to 2. This way you have created the circular linked list .So for this you can maintain a map or a visited array…whenever you see that a node is already visited, connect next pointer of previous node to that node.
After that just detect the cycle , remove the cycle, print the new linked list.
You can also refer this:
And what if I have duplicates elements in ll?
Here in this question and according to the testcase…whenever you see a duplicate or a node that is already visited…it means that a circular ll have been formed from these…as i explained before.
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.