codelink:https://pastebin.com/vZmjHX52
what’s wrong with my code please correct it
Circular linked list code problem
Hello @Divya_321,
The linked list you have formed is structurally linear.
There is no actual loop or cycle in it.
Thus, the fast and slow pointers will not work for the linked list you have created.
Solution:
- just look for the repeating element in your linked list and break it from there to remove cycle.
- to apply two pointers approach that you have written, create a cycle in your linked list.
You can refer to this code I have corrected earlier:
https://ide.codingblocks.com/s/161841
Hope, this would help.
Give a like if you are satisfied.
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.
Hey I got your code , but I have a question why we comparing occurrence of same data inspite of address like here also you compared if same data occurred or not but in actual case it is when same address is found than there is a cycle and not occurrence of same data indicates cycle but same as in this question I got some few more question where repeated data is treated as an indication of cycle, do we always take repeated data to identify cycle and not address please clear this??? because in my approach I tried to reassign the same address to form the cycle where I got repeated data like for 1->2 ->3->4->5->2->->3 was given I connected next of node containing 5 to 2 to create the cycle than I tried to get if same address occurred or not for cycle detection, please specify the approach of the problem
Hello @Divya_321,
This is because in the question the cycle is marked with repeating element.
As you can see the same testcase.
That’s the reason we are checking for data.
But you can also check for the address but this would work only if there is an actual cycle present in the linked list.
Then only the address of node will repeat.
At the time of creating tree in this question.
You will check if the data would repeat, create a cycle.
Hope, i have answered your doubt.
yes now it’s clear! thanks