Whats the mistake?
Hello @garganshul151,
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 donot understand thecode that you have provided
plz explain the mistake in my code and do correct it
Hello @Neeta,
Mistake in your code:
The linked list you have formed is structurally linear i.e. sequential
There is no actual loop or cycle in it.
The fast and slow pointers logic works when there is a loop in the list.
Thus, the fast and slow pointers will not work for the linked list you have created.
Solution:
- keep track of duplicates.
- if a duplicate is detected, then make the next of previous node to point to the node containing the first occurrence of that duplicate value.
- This way, you would have a cycle in your linked list.
Now, you can apply the two-pointer logic in this loop.
Try to code it yourself.
If you face any difficulty, let me know.
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.