We are given a linked list which contains a cycle. Detect that cycle and break it. Print the linked list after removing the cycle. NO OUTPUT IS PRINTING?

error in code ??

you are not creating a cycle at all, so eventually fast becomes NULL and accessing it’s next is an error
secondly, ur logic for breaking the cycle is wrong and also for printing
how to break the cycle is
store a prev = NULL
while(fast!=slow)
{
prev = fast;
fast = fast->next;
slow = slow->next;
}
prev->next = NULL
this breaks the cycle

my logic is different…i m actually just finding the point where the cycle is happening ie where the elements again start to get repeated…
and thus printing only till that stored address ie where the elements again start to get repeated

now please see wrt this logic
and not doing fast!=null instead only while(1) since cycle is present for sure therefore it while will break for sure

node* temp=head;
int c=0;
while(temp!=NULL && c>1) 
{
	if(temp==slow) c++;
	cout<<temp->data<<" ";
	temp=temp->next;
}

here c is never >1 before entering the loop, it will not enter the loop
and your code does not have the cycle. u will have to create the cycle, else the inputs will just keep on coming in, like this, fast and slow will not be equal ever.
maybe in some rare case there data will be equal
think of the input as simply an array, now create a cycle in the linked list according to that array and then print

now the question arises how to create the cycle,
u can do it by storing each unique element and when the first occurence of a repitition happens, point the current last element to the repeated element
alternately, simply maintain repeated elements and print until an element is not repeated

dry run your code for the sample input
1 2 3 4 5 2 3 -1
fast is at 1
slow at 1
then fast goes to 3 and then 5 and then 2
slow goes to 2 3 and then 4
at any instance they never get equal, even if their datas would have been equal, their address wouldn’t
and hence fast will ultimately run to null and create a runtime error when u access it’s next

if i do c<1 yeah this is my mistake
then the code shows
/bin/run.sh: line 4: 18 Segmentation fault (core dumped) ./exe

please can you tell how my logic is wrong ?? just see it once please
what i have done i explained above !
thank u sir

dry run your code for the sample input
1 2 3 4 5 2 3 -1
fast is at 1
slow at 1
then fast goes to 3 and then 5 and then 2
slow goes to 2 3 and then 4
at any instance they never get equal, even if their datas would have been equal, their address wouldn’t
and hence fast will ultimately run to null and create a runtime error when u access it’s next
look at this reply please this is the fault, your code will not be able to detect the repeated element
it check slow == fast yes
but they are pointers not values
same values get assigned diff addresses not the same
and even if they do, this still does not match since your code does not contain a cycle, u will have to create a cycle manually, as an input u are given an array not a cycled ll, u have create that urself

ohh yeah sorry i didn’t notice but the floyd’s algo wont work here ???

It will work if u create a cycle, otherwise keep a map of elements and when a repeated element is found stop printing there

yeah thats what !! thanks :slight_smile:

@hg11110000 if your doubt is solved. please mark it as solved. happy to help :slight_smile: