how to create a linked list for cycle detection as we need to connect our last node to some node
Linked list creation problem
you can use set or hashmap for this
take input if any input repeats then set size will not increase(as set stores only different values) this is the point where you have to create cycle
you can see the compete code in Reference code if needed
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask
please check my code
you have done mistake inside innermost while loop
correct condition is fast->next!=slow->next
correct function is
bool floydCycleRemoval(Node *head)
{
/* Code here */
if(head==NULL or head->next==NULL){
return false;
}
Node *slow = head;
Node *fast = head;
while(fast!=NULL and fast->next!=NULL){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
// Node *temp = slow;
slow = head;
while(fast->next != slow->next){
fast = fast->next;
slow = slow->next;
}
fast->next = NULL;
return true;
}
}
return false;
}
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask
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.