Giving wrong answer here

https://ide.codingblocks.com/s/66938
https://hack.codingblocks.com/contests/c/53/471

HI Gaurav, the removeCycle function is causing error for large inputs. You can print the answer even without performing removeCycle. What you can do is after you get the value of isCycle node in main store its value in a variable, say x. i.e.

int x = isCycle->data ;

then when you call print, pass x also along with node i.e.

print(head,x) ;
// and hence change your function definition to
void print(node* head, int x) { … }

Now what you can do is keep on printing the linked list normally and stop and when you encounter x for the second time. This can be done using a bool variable as:

bool f = 0 ;
while ( head != NULL ) {
if (head->data == x ) {
if ( f==0) f = 1;
else return ;
}

}

Try to understand what is happening using f. Hope this helps :slight_smile:

got it…but still If I want to remove cycle what will be the code

your apprach is not working
https://ide.codingblocks.com/s/67119

Hi Gaurav, for deleting the cycle it’s very simple. After you have printed your last element make it’s next as NULL. And for this you have to keep track of the previous node as well and thus you have to use 2 pointers where one stays behind the other and before you return (in my last message) just set prev->next = NULL.

P.S.: Deleting is not required here, just the printing part is required. We may have deleted the extra/repeating node in cycle if further more processing was required in program; to make it efficient.

Hope this helps :slight_smile:

For this, there is no need of using isCycle function also. Just do this:

node* isCycle=detectCycle(head);
int x=isCycle->data;
print(head,x);

No need of that if block in lines 145 - 158.
Make this modification and it will work then. :slight_smile:

still wrong …please update this code with comments
https://ide.codingblocks.com/s/67119

i have marked some comments
https://ide.codingblocks.com/s/67170
i have modified the code
see this’
https://ide.codingblocks.com/s/67168

Hey Gaurav,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.

https://ide.codingblocks.com/s/68728
giving 2 runtime error
https://hack.codingblocks.com/contests/c/53/471