some of the test case is wrong
Detect and remove cycle
Your code is wrong for the test cases or the test cases are wrong? If test cases are wrong, can you please tel which test cases?
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void removeLoop(struct Node*, struct Node*);
int detectAndRemoveLoop(struct Node* list)
{
struct Node *slow_p = list, *fast_p = list;
while (slow_p && fast_p && fast_p->next) {
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p) {
removeLoop(slow_p, list);
}
}
}
void removeLoop(struct Node* loop_node, struct Node* head)
{
struct Node* ptr1;
struct Node* ptr2;
ptr1 = head;
while (1) {
ptr2 = loop_node;
while (ptr2->next != loop_node && ptr2->next != ptr1)
ptr2 = ptr2->next;
if (ptr2->next == ptr1)
break;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
}