after slow and fast meet 2nd time then how to reach to the prev element of fast pointer??
Removal of cycle in a linked list
Hello @S19LPPP0192,
You can do this in two ways:
-
Either take a temporary pointer variable of type node to keep track of previous element.
slow=head;
node* prev=NULL;
while(slow != prev) {
slow=slow->next;
prev=fast; // Track of previous node.
fast=fast->next;
} -
Without using temporary variable:
slow=head;
while(slow->next!=fast->next){
slow=slow->next;
fast=fast->next;
}
Hope, this would help.
Give a like, if you are satisfied.
May i know, if you have some doubt?
As you have reopened this doubt.
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.