we are given input like 1 2 3 4 5 2 3 -1
So we have to make a cycle from 5 to 2 because after 5 , 2 and 3 are repeating. So we have to make a cycle. And I have initialized prev in the lower part of the outer for loop. Prev-> next should not give an error here. Even if I am initializing prev with head, it is giving the same error.
Circular linked list
Pls check my code from the line 37 to 52. code is at the link https://ide.codingblocks.com/s/233769 . I am unable to make the cycle in the linked list. It is showing runtime error.
pls someone solve my doubt
hey, @ankur8632 the problem is
while(fast!=slow){
fast=fast->next->next;
slow=slow->next;
}
this first loop
here both head and slow are initialised to head, so this loop never runs and hence cycle is never detected and none of other things happen as well and hence the error
everything else is correct
how to change this is, simply run for while(fast) and break inside when both slow and fast are equal