Cycle detection and removal - Linked List

Here’s my code - https://ide.codingblocks.com/s/411854

One of the test case is showing run error, please help me out.

Hello @gambhirrahul0, just a small change,

    while(fast != NULL || fast -> next != NULL){
        fast = fast -> next -> next;
        slow = slow -> next;
        if(fast == slow){
            flag=true;
            break;
        }
    }
// you have written this code segment but in the while condition, we must use && instead of ||

So the correct one is,

    while(fast != NULL && fast -> next != NULL){
        fast = fast -> next -> next;
        slow = slow -> next;
        if(fast == slow){
            flag=true;
            break;
        }
    }

I hope it is clear to you if still there is any doubt pls let me know.
Thanks :slight_smile:

It worked, thanks. May I know the reason/explanation, please? Thanks in advance.

I got it, my bad. Thanks, I’m closing the doubt.

1 Like

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.