i have Written my code for removal of Linked list cycle but don’t know if it is correct or not so can you check it (code - “https://ide.codingblocks.com/s/102282”) and fucntion of removal is at line - #275
Is My Code Correct for Floyd cycle?
You can check your code here.
https://hack.codingblocks.com/contests/c/53/471
Click on the link and you will find a question related same.
Submit it there and check if it working correct or not.
Suggestion: Read the input and output format of the question.
Let me know, if you face any problem.
i have attempted that question but getting Runtime Error . whats wrong here - “https://ide.codingblocks.com/s/102528”
The logic you are applying for removing cycle is wrong.
As, you know that you are building a linear linked list.
So, there is no cycle exist structurally.
Hence, that slow-fast method would not work here.
Your function detectCycle() will always return NULL.
Suggestion:
- traverse the linked list linearly and at every node check if the node->next->data has already occurred in the left side of that node.
- if yes, then make node->next=NULL. This will remove the rest of the nodes and hence, the cycle.
- if not, move one node ahead and repeat the same.
Hope, this would help.