# Detect and Remove Loop in the Linked List

Detect and Remove Loop in the Linked List” is one of the most asked problem asked in technical and algorithmic interview. This problem is an extension of problem “Detect Loop in the Linked List”. In this problem, we need to first detect whether there exists a loop in the linked list or not. If loop exist, then we need to remove the loop from the linked list.

For example, we need to remove loop from the following linked list:

After Removing the loop, the linked list becomes:

To do so, we have basically two methods.

1. Detect and remove loop by hashing method.
2. Detect and remove loop by Floyd’s loop detection algorithm (without counting).

#### Method 1: Detect and remove loop in the linked list by hashing method:

The steps required to remove a loop from a linked list are as follows:

1. Detect whether loop exist in a linked list or not using hash based method discussed in this post.
2. If loop exist, then our temporary pointer will point to the beginning node of the loop.
3. We will traverse the entire loop and store the previous node in a temporary pointer node known as ‘prev’.
4. We will then make pointer of ‘prev’ node, pointing to NULL.
5. If loop does not exist, we will simply return.

#### C++  Program to detect and remove loop in the linked list using hashing methodis as follows:

```/* C++ Program to detect and remove loop in the linked list */
#include<bits/stdc++.h>
using namespace std;

/* Structure of the Node of the Linked List */
struct node
{
int data;
struct node *next;
};

/* Function to insert node at the end of the linked list */
void insert_end(struct node **head, int ele)
{
/* Creating a new node */
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* check whether the linked list is empty or not */

{
return;
}
/* If Linked List is not empty, traverse the complete linked list and point the pointer
of last node of the linked list to the new node*/
struct node *temp = NULL;
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}

/* A Utility function to create a loop in the linked list */
{
while(temp->next !=NULL)
temp = temp->next;
}

/* Function to Detect and remove Loop from the Linked List */
{
map<struct node*,int> mp;
while(temp!=NULL)
{
mp[temp]++;
if(mp[temp] == 2) // Loop Exist
{
struct node *p = temp;
while(p->next != temp)
{
p = p -> next;
}
p->next = NULL;
return;
}
temp = temp->next;
}
}

/* Function to detect Loop in a Linked List */
{
map<struct node*, int> mp;
while(temp!=NULL)
{
mp[temp]++;
if(mp[temp]==2)
{
cout<<"Loop Exist\n";
return;
}
temp = temp->next;
}
cout<<"Loop does not Exist\n";
}

/* Function to Print Linked List */
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
/* Insert Some Nodes in the Linked List */
/* Call Utility Function to create Loop in a Linked List */

/* Call function to Detect whether loop exists in the linked list or not */

/* Call Function to remove loop from the linked list, if exists  */
cout<<"Removing Loop, if Exist!!\n";

/* After Removing Loop Again, checking wheher loop exist or not in the linked list */

/* Print the Linked List */
cout<<"\nThe Elements of the Linked List are:\n";
}
```
```OUTPUT:
Loop Exist
Removing Loop, if Exist!!
Loop does not Exist
The Elements of the Linked List are:
4 3 2 1```

#### Method 2: Detect and remove loop in the linked list by Floyd’s loop detection algorithm:

The steps required to detect and remove loop in the linked list are as follows:

1. Detect whether loop exist in a linked list or not using Floyd’s cycle detection algorithm.
2.  If loop exists in the list, point the ‘slow’ pointer to the head of the list, then move ‘slow’ and ‘fast’ pointer as same speed.
3.  The ‘slow’ and ‘fast’ pointers will meet at the beginning of the loop.
4. We again traverse the list with fast pointer till the node next to fast pointer is pointing to slow pointer.
5. Then, we will point the fast pointer to NULL.

#### C++ Program to detect and remove loop in the linked list using Floyd’s loop detection method is as follows:

```/* C++ Program to Detect and Remove Loop of the Linked List */
#include<bits/stdc++.h>
using namespace std;

/* Structure of the Node of the Linked List */
struct node
{
int data;
struct node *next;
};

/* Function to Insert Node at the Linked List */
void insert_end(struct node **head, int ele)
{
/* Creating a new node */
struct node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;

/* check whether the linked list is empty or not */

/* If Linked List is empty, then new node will become head of the linked list */
{
return;
}

/*
If Linked List is not empty, then traverse the complete linked list and points the pointer
of the last node of the linked list to the new node
*/
struct node *temp = NULL;
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}

/* A Utility Function to create loop in the linked list */
{
while(temp->next !=NULL)
temp = temp->next;
}

/* Function to detect and remove loop of the linked list */
{
/* Initialize two pointers; slow pointer which will traverse the linked list one node at a time
and fast pointer which will traverse the linked list two nodes at a time */
while(slow!=NULL && fast!=NULL && fast->next!=NULL)
{
slow = slow->next;
fast = fast->next->next;
/* If at any time, the slow pointer and faster pointer meets,
then there exists a loop in the linked list  */
if(slow == fast)
{
/* The slow and fast pointer meets at beginning of the loop of the linked list*/
while(slow!=fast)
{
slow = slow->next;
fast = fast->next;
}
/* Traverse the loop and point the pointer of last node of loop to the NULL */
while(fast->next!=slow)
{
fast = fast->next;
}
fast->next = NULL;
return;
}
}
}

/* Function to detect whether loop exists in the linked list */
{
/* Creating a HashMap of 'Node type' as Key and 'int' as pair */
map<struct node*, int> mp;
/*
Traverse the linked list and insert nodes in the Hashmap.
While Inserting, check whether the node already exist or not.
If node already exists, then there exists a loop in the linked list.
Else, there does not exists a loop in the linked list.
*/
while(temp!=NULL)
{
mp[temp]++;
if(mp[temp]==2)
{
cout<<"Loop Exist\n";
return;
}
temp = temp->next;
}
cout<<"Loop does not Exist\n";
}

/* Function to print the linked list */
{
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
/* Insert some nodes of the linked list */
/* Call Utility Function to create Loop in a Linked List */

/* Call function to Detect whether loop exists in the linked list or not */

/* Call Function to remove loop from the linked list, if exists  */
cout<<"Removing Loop, if Exist!!\n";

/* After Removing Loop Again, checking wheher loop exist or not in the linked list */

/* Print the Linked List */
```OUTPUT: