# Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false if there is no loop in the list. The list below must be changed to 1 -> 2 -> 3 -> 4 -> 5 -> NULL via the detectAndRemovetheLoop() function.

We must first find the loop in order to attempt to delete it. To end the loop, all that is needed is a pointer to the last node. As in the node with the value 5 above. As soon as we are aware of the pointer to the last node, we can declare the following node in this loop to be NULL.

To obtain the pointer to the last node, we can simply utilise the hashing or visited node approaches. The basic principle is that the final node is the first node whose next has already been visited or hashed.

The Floyd Cycle Detection technique can be used to find and get rid of the loop as well. Floyd's algorithm's loop node is where the slow and fast pointers combine. This loop node can be used to get rid of the cycle. When Floyd's technique is used to detect loops, there are two possible approaches to break the loop.

How may a loop be found in a linked list?

The fast and slow pointer approach, in which the fast pointer advances by two nodes and the slow pointer moves by one node at a time, can effectively find loops.

Technique 1 (Check one by one): We are aware that Floyd's Cycle detection procedure ends when the fast and slow pointers collide at the same location. We also understand that this point is a loop node. In a pointer variable, such as ptr2, save the address of this. Then, beginning at the top of the Linked List, determine which nodes may be reached from ptr2 by checking them one at a time. It is possible to obtain the pointer to the preceding node of any reachable node in the Linked List, indicating that this node represents the beginning of the loop.

• Get the pointer to a loop node by utilising Floyd's Cycle detection technique to find a loop.
• Count how many nodes are in the loop. Let k be the count.
• One pointer should be fixed to the head, and the other to a kth node from the head.
• The pointers will collide at the loop's starting node if we move them both at the same speed.
• Obtain a pointer to the loop's final node, and make the next one NULL.

C++ Program:

``````#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data1;
struct Node* next1;
};
void removetheLoop ( struct Node*, struct Node* );
int detectAndRemovetheLoop ( struct Node* list )
{
struct Node *slow_po = list, *fast_po = list;
while ( slow_po && fast_po && fast_po -> next1 )
{
slow_po = slow_po -> next1;
fast_po = fast_po -> next1 -> next1;
if ( slow_po == fast_po )
{
removetheLoop ( slow_po, list );
return 1;
}
}
return 0;
}
void removetheLoop ( struct Node* loop_node, struct Node* head )
{
struct Node* ptr1 = loop_node;
struct Node* ptr2 = loop_node;

unsigned int k = 1, i;
while ( ptr1->next1 != ptr2 )
{
ptr1 = ptr1->next1;
k++;
}
for ( i = 0; i < k; i++ )
ptr2 = ptr2->next1;
while ( ptr2 != ptr1 )
{
ptr1 = ptr1->next1;
ptr2 = ptr2->next1;
}

while ( ptr2->next1 != ptr1 )
ptr2 = ptr2->next1;
ptr2->next1 = NULL;
}
void printList ( struct Node* node )
{
while ( node != NULL )
{
cout << node -> data1 << " ";
node = node -> next1;
}
}
struct Node* newNode ( int key )
{
struct Node* temp = new Node();
temp -> data1 = key;
temp -> next1 = NULL;
return temp;
}
int main()
{
struct Node* head = newNode (60);
head -> next1 = newNode (30);
head -> next1 -> next1 = newNode (25);
head -> next1 -> next1 -> next1 = newNode (14);
head -> next1 -> next1 -> next1 -> next1 = newNode (20);
head -> next1 -> next1 -> next1 -> next1 -> next1 = head -> next1 -> next1;
cout << "After the loop has been removed, the Linked List : \n";
return 0;
}
``````

Output:

``````After the loop has been removed, the Linked List :
60 30 25 14 20
``````

Technique 2 (Loop without Counting Nodes): There is no requirement to count the nodes in a loop. After identifying the loop, if we move the fast and slow pointers at the same speed until the fast pointers don't meet, they will collide at the beginning of the loop.

How does that function?

Let the Floyd's Cycle finding algorithm lead to a point where slow and fast will collide. The scenario when the cycle is discovered is depicted in the diagram below.

C++ Program:

``````#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int keys;
struct Node* next1;
}
Node;

Node* newNode ( int keys )
{
Node* temp = ( Node* )malloc( sizeof (Node) );
temp -> keys = keys;
temp -> next1 = NULL;
return temp;
}
void printtheList ( Node* head )
{
while ( head != NULL )
{
printf ( "%d ", head->keys) ;
}
printf ("\n");
}
void detectAndRemovetheLoop ( Node* head )
{
if ( head == NULL || head -> next1 == NULL )
return;

slow = slow -> next1;
fast = fast -> next1 -> next1;

while ( fast && fast -> next1)
{
if ( slow == fast )
break;
slow = slow -> next1;
fast = fast -> next1 -> next1;
}
if  (slow == fast )
{

if ( slow == fast )
while ( fast -> next1 != slow )
fast = fast -> next1;
else
{
while ( slow -> next1 != fast -> next1 )
{

slow = slow -> next1;
fast = fast -> next1;
}
}
fast -> next1 = NULL;
}
}
int main ()
{
head -> next1 = newNode (30);
head -> next1 -> next1 = newNode (25);
head -> next1 -> next1->next1= newNode (14);
head -> next1 -> next1->next1->next1 = newNode (20);
head -> next1 -> next1 -> next1 -> next1 -> next1 = head;
printf ("After the loop has been removed, the Linked List : \n");
return 0;
}
``````

Output:

``````After the loop has been removed, the Linked List :
60 30 25 14 20
``````

Technique 3 (Hashing: Hash the addresses of the nodes in the linked list): In an unordered map, we may simply check to see if an element already exists by hashing the addresses of the linked list nodes. If it exists, then we have reached a node that already exists after a cycle and must set the following node's reference to NULL.

C++ Program:

``````#include <bits/stdc++.h>
using namespace std;

struct Node
{
int keys;
struct Node* next1;
};
Node* newNode ( int keys )
{
Node* temp = new Node;
temp -> keys = keys;
temp -> next1 = NULL;
return temp;
}
void printtheList ( Node* head )
{
while ( head != NULL )
{
cout << head -> keys << " ";
}
cout << endl;
}
void hashAndRemoveit ( Node* head )
{
unordered_map<Node*, int> node_map;

Node* last = NULL;
while ( head != NULL )
{
if ( node_map.find(head) == node_map.end() )
{
}
else {
last -> next1 = NULL;
break;
}
}
}
int main ()
{
head -> next1 = newNode (30);
head -> next1 -> next1 = newNode (25);
head -> next1 -> next1 -> next1 = newNode (14);
head -> next1 -> next1 -> next1 -> next1 = newNode (20);

head -> next1 -> next1 -> next1 -> next1 -> next1 = head -> next1 -> next1;
``````After the loop has been removed, the Linked List :