Here goes my code.
Although I removed the cycle butstill it’s printing the while list with loop
Printing the complete list
there was no cycle formed in ur code, so i have formed a cycle, ur code gave the correct output for odd number of nodes and wrong for even. why?
see, consider 1 2 3 4 5 6 7
for fast pointer it goes to 1 3 5 and then to 7 where the loop breaks, so prev gets assigned to 7 and hence it is correct
but now consider 1 2 3 4 5 6
fast pointer goes to 1 3 5 and then NULL, so prev remains as NULL and at the end of the function u refer to prev->next which is a runtime error
what i have done in my code? added a cycle
there is one more check that u need to put
if aftr breaking the first loop the fast pointer==head that means the entire list is cycled, and slow and fast will be both at head then in the second loop it will break after just the first iterartion and u will lose the reference to the entire linked list which is wrong, i have left this one small task for u to tackle so that u understand the concept better
Sir I didn’t understand the logic for the cycle. In the cycle code you added(temp->next=head->next->next) which means the last node will be pointing to 3rd node.
@tanjuljain19 that was just random cycle creation to just show you how the code will work, u can try making it anywhere in the linked list
if your doubt is solved, please mark it as resolved