Node* detectCycle(Node* head)
{
Node* fast=head, * slow=head;
while(fast->next!=NULL)
{
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
{
return fast;
}
}
return NULL;
}
bool floydCycleRemoval(Node *head)
{
/* Code here */
if(head==NULL|| head->next==NULL)
{
return false;
}
Node* intersection=detectCycle(head);
if(
intersection!=NULL
)
{
Node * slow=head;
while(slow!=intersection)
{
slow=slow->next;
intersection=intersection->next;
}
while(slow->next!=intersection)
{
slow=slow->next;
}
slow->next=NULL;
return true;
}
else{
return false;
}
}