Linked List Floyd algorithm cycle detection

i have done the code but how do i test it for cycled loop i am not able to give input which has cycle my code is working fine for linked list that contains no cycle
here is my code https://ide.codingblocks.com/s/287845

https://hack.codingblocks.com/app/contests/1281/1307/problem

its not working there showing time limit exceeded

yar can u tell me error in my code instead of giving me the code you can tell me the errors so i can learn

sir u are not removing the cycle
when it is detected

and in the link that i provided u just need to add code in flyod

if u havent seen the video of how to remove cycle then please see that then solve this q

else the simple way to create cycle in ur linked list is to do
head->next = head->next->next


here i am removing the cycle by going to the prev and making its next point to null i already did that mam

image

where have u consulted this logic from?

i dont find it correct


to make a loop do something like
image

this logic was explained in the video by sir he said in the first step we have to find whether the LL is cycled or not to find that i did
nodefast=head;
node
slow=head;
//1 step: check for the cycle
while(fast!=NULL && fast->next!=NULL){
slow = slow->next;
fast=fast->next->next;
if(slow==fast){
cout<<“Cycle is Present”<<endl;
break;
}
else{
return head;
}
}
//if cycle is present i have to break it by finding the head of the cycle and finding its previous node by making it point to null so he said in the first step when slow = head it means cycle is there then to remove it make slow ptr point to head and keep fast ptr as it is and then make both the pointers move one step when they meet at same point it is the head of the LL then make its previous point to null
//2.step:break the cycle

slow=head;

node*prev=NULL;

while(slow!=fast){

    prev=fast;

    slow =slow->next;

    fast=fast->next;

}

prev->next=NULL;

return head;

so i want to know where is my logic wrong its working fine when there is no cycle present

u where doing else condition in if( slow == fast)
vha nhi karna
i have added comments and also
shown the style to form loop
now code works for cycles linked list as well

yeah i understood it but when slow == fast and i want to know at which node cycle is present i do slow->data and it gives me the wrong node?

code meh mene
slow->next == fast->next

to break hoga
fir cout << slow->next->data

to sahi answer ayega

yaar aapne cycle 15 pe banaya haina???

toh vo answer aa he ni raha u can check

aapne 10 ke next mai 15 ka add daala hai

20 peh bana h n a cycle to

delte ho to rha h cycle
verna infinte loop peh chale jata
print ho to rahi h

toh 15 pe cycle hua naa toh vo print hona chaiye?

head->next->next->next->next->next = head->next->next;
50 20 15 4 10
iske according toh 10 ke next mai 20 ka next hai naa jo ki 15 hai?