In the above mentioned probelem, I am unavle to figure out my mistake. Only 2 tests cases pass, rest either give a TLE aor a Run Error. Please Help
Cycle Detection and Removal in Linked List
@Yash5646
I have modified your code slightly.
There was one mistake. Fast had to be set to head and the while loop only needed two conditions
while(fast != NULL and fast->next != NULL). Fast had to be set to head because otherwise the entire logic behind Floyd’s Algorithm fails.
The rest of the code was fine.
If my reply was able to clear your doubt, then please mark your query as resolved.
Please refer to this code. There were some issues in your code. Firstly, initially , slow and fast have the same values. But the (slow == fast) checker was present in the beginning of while loop. Thus it was given no chance to execute and we broke out of the loop in the very first step leading to a TLE.
Secondly, the condition in the loop when fast was reset to head after slow and fast turned out to be equal at some point. The condition should have been while(fast->next != slow->next) instead of the latter one. Because according to the algorithm, both the pointers are moved simultaneously. Also because fast was set to head, slow->next must point to NULL. Please try working out an example on paper and that would make things more clear for you.
You can refer to the code which I have attached above.
If my answer is able to resolve your query, please mark the doubt as resolved.