I am unable to remove the cycle from the link list. I am unable to understand what is the bug in the program pls help me to solve it.
Removing cycle from linked list
hello @guptamuskan1305
pls save ur code here -> https://ide.codingblocks.com/
and share the link with me
you are not reading input correctly.
this how u should take input for list which can have cycle
Say If INPUT IS 1 2 3 4 5 2 3 -1
form a linked list 1->2->3->4>5 then you see that 2 has already been visited (So for this you can maintain a map or a visited array…whenever you see that a node is already visited, connect next pointer of previous node to that node.) so connect the next pointer of 5 to 2. This way you have created the circular linked list .
void buildCycleList(Node *&head)
{
unordered_map<int, Node *> hash;
int x;
cin >> x;
if (x == -1)
{
head = NULL;
return;
}
head = new Node(x);
hash[x] = head;
Node *current = head;
while (x != -1)
{
cin >> x;
if (x == -1)
break;
if (hash.find(x) != hash.end())
{
current->next = hash[x];
continue;
}
Node *n = new Node(x);
current->next = n;
current = n;
hash[x] = n;
}
current->next = NULL;
}
I don’t need to create a cycle I need to delete one
yeah you have to delete cycle ,but for that first u need to build cyclic linked list from the given input, and for that u should follow what i said above