# Detect and Remove Loop in a Linked List

Write a function detectAndRemoveLoop() that checks whether a given Linked List contains loop and if loop is present then removes the loop and returns true. And if the list doesn’t contain loop then returns false. Below diagram shows a linked list with a loop. detectAndRemoveLoop() must change the below list to 1->2->3->4->5->NULL.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

We also recommend to read following post as a prerequisite of the solution discussed here.

Write a C function to detect loop in a linked list

Before trying to remove the loop, we must detect it. Techniques discussed in the above post can be used to detect loop. To remove loop, all we need to do is to get pointer to the last node of the loop. For example, node with value 5 in the above diagram. Once we have pointer to the last node, we can make the next of this node as NULL and loop is gone.
We can easily use Hashing or Visited node techniques (discussed in the above mentioned post) to get the pointer to the last node. Idea is simple: the very first node whose next is already visited (or hashed) is the last node.
We can also use Floyd Cycle Detection algorithm to detect and remove the loop. In the Floyd’s algo, the slow and fast pointers meet at a loop node. We can use this loop node to remove cycle. There are following two different ways of removing loop when Floyd’s algorithm is used for Loop detection.

Method 1 (Check one by one)
We know that Floyd’s Cycle detection algorithm terminates when fast and slow pointers meet at a common point. We also know that this common point is one of the loop nodes (2 or 3 or 4 or 5 in the above diagram). We store the address of this in a pointer variable say ptr2. Then we start from the head of the Linked List and check for nodes one by one if they are reachable from ptr2. When we find a node that is reachable, we know that this node is the starting node of the loop in Linked List and we can get pointer to the previous of this node.

## C

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

struct Node
{
int data;
struct Node* next;
};

/* Function to remove loop. Used by detectAndRemoveLoop() */
void removeLoop(struct Node *, struct Node *);

/* This function detects and removes loop in the list
If loop was there in the list then it returns 1,
otherwise returns 0 */
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 and fast_p meet at some point then there
is a loop */
if (slow_p == fast_p)
{
removeLoop(slow_p, list);

/* Return 1 to indicate that loop is found */
return 1;
}
}

/* Return 0 to indeciate that ther is no loop*/
return 0;
}

/* Function to remove loop.
loop_node --> Pointer to one of the loop nodes
head -->  Pointer to the start node of the linked list */
void removeLoop(struct Node *loop_node, struct Node *head)
{
struct Node *ptr1;
struct Node *ptr2;

/* Set a pointer to the beging of the Linked List and
move it one by one to find the first node which is
part of the Linked List */
while (1)
{
/* Now start a pointer from loop_node and check if it ever
reaches ptr2 */
ptr2 = loop_node;
while (ptr2->next != loop_node && ptr2->next != ptr1)
ptr2 = ptr2->next;

/* If ptr2 reahced ptr1 then there is a loop. So break the
loop */
if (ptr2->next == ptr1)
break;

/* If ptr2 did't reach ptr1 then try the next node after ptr1 */
ptr1 = ptr1->next;
}

/* After the end of loop ptr2 is the last node of the loop. So
make next of ptr2 as NULL */
ptr2->next = NULL;
}

/* Function to print linked list */
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d  ", node->data);
node = node->next;
}
}

struct Node *newNode(int key)
{
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = key;
temp->next = NULL;
return temp;
}

/* Drier program to test above function*/
int main()
{

/* Create a loop for testing */

printf("Linked List after removing loop \n");
return 0;
}
```

## Java

```// Java program to detect and remove loop in linked list

static class Node {

int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

// Function that detects loop in the list
int detectAndRemoveLoop(Node node) {
Node slow = node, fast = node;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;

// If slow and fast meet at same point then loop is present
if (slow == fast) {
removeLoop(slow, node);
return 1;
}
}
return 0;
}

// Function to remove loop
void removeLoop(Node loop, Node curr) {
Node ptr1 = null, ptr2 = null;

/* Set a pointer to the beging of the Linked List and
move it one by one to find the first node which is
part of the Linked List */
ptr1 = curr;
while (1 == 1) {

/* Now start a pointer from loop_node and check if it ever
reaches ptr2 */
ptr2 = loop;
while (ptr2.next != loop && ptr2.next != ptr1) {
ptr2 = ptr2.next;
}

/* If ptr2 reahced ptr1 then there is a loop. So break the
loop */
if (ptr2.next == ptr1) {
break;
}

/* If ptr2 did't reach ptr1 then try the next node after ptr1 */
ptr1 = ptr1.next;
}

/* After the end of loop ptr2 is the last node of the loop. So
make next of ptr2 as NULL */
ptr2.next = null;
}

// Function to print the linked list
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}

// Driver program to test above functions
public static void main(String[] args) {

// Creating a loop for testing
System.out.println("Linked List after removing loop : ");
}
}

// This code has been contributed by Mayank Jaiswal
```

## Python

```
# Python program to detect and remove loop in linked list

# Node class
class Node:

# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def detectAndRemoveLoop(self):
while(slow_p and fast_p and fast_p.next):
slow_p = slow_p.next
fast_p = fast_p.next.next

# If slow_p and fast_p meet at some poin
# then there is a loop
if slow_p == fast_p:
self.removeLoop(slow_p)

# Return 1 to indicate that loop if found
return 1

# Return 0 to indicate that there is no loop
return 0

# Function to remove loop
# loop node-> Pointer to one of the loop nodes
# head --> Pointer to the start node of the
def removeLoop(self, loop_node):

# Set a pointer to the beginning of the linked
# list and move it one by one to find the first
# node which is part of the linked list
while(1):
# Now start a pointer from loop_node and check
# if it ever reaches ptr2
ptr2 = loop_node
while(ptr2.next!= loop_node and ptr2.next !=ptr1):
ptr2 = ptr2.next

# If ptr2 reached ptr1 then there is a loop.
# So break the loop
if ptr2.next == ptr1 :
break

ptr1 = ptr1.next

# After the end of loop ptr2 is the lsat node of
# the loop. So make next of ptr2 as NULL
ptr2.next = None
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)

def printList(self):
while(temp):
print temp.data,
temp = temp.next

# Driver program
llist.push(10)
llist.push(4)
llist.push(15)
llist.push(20)
llist.push(50)

# Create a loop for testing

llist.detectAndRemoveLoop()

print "Linked List after removing loop"
llist.printList()

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

Output:
```Linked List after removing loop
50 20 15 4 10 ```

Method 2 (Better Solution)
This method is also dependent on Floyd’s Cycle detection algorithm.
1) Detect Loop using Floyd’s Cycle detection algo and get the pointer to a loop node.
2) Count the number of nodes in loop. Let the count be k.
3) Fix one pointer to the head and another to kth node from head.
4) Move both pointers at the same pace, they will meet at loop starting node.
5) Get pointer to the last node of loop and make next of it as NULL.

Thanks to WgpShashank for suggesting this method.

## C

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

struct Node
{
int data;
struct Node* next;
};

/* Function to remove loop. */
void removeLoop(struct Node *, struct Node *);

/* This function detects and removes loop in the list
If loop was there in the list then it returns 1,
otherwise returns 0 */
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 and fast_p meet at some point then there
is a loop */
if (slow_p == fast_p)
{
removeLoop(slow_p, list);

/* Return 1 to indicate that loop is found */
return 1;
}
}

/* Return 0 to indeciate that ther is no loop*/
return 0;
}

/* Function to remove loop.
loop_node --> Pointer to one of the loop nodes
head -->  Pointer to the start node of the linked list */
void removeLoop(struct Node *loop_node, struct Node *head)
{
struct Node *ptr1 = loop_node;
struct Node *ptr2 = loop_node;

// Count the number of nodes in loop
unsigned int k = 1, i;
while (ptr1->next != ptr2)
{
ptr1 = ptr1->next;
k++;
}

// Fix one pointer to head

// And the other pointer to k nodes after head
for (i = 0; i < k; i++)
ptr2 = ptr2->next;

/*  Move both pointers at the same pace,
they will meet at loop starting node */
while (ptr2 != ptr1)
{
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}

// Get pointer to the last node
ptr2 = ptr2->next;
while (ptr2->next != ptr1)
ptr2 = ptr2->next;

/* Set the next node of the loop ending node
to fix the loop */
ptr2->next = NULL;
}

/* Function to print linked list */
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d  ", node->data);
node = node->next;
}
}

struct Node *newNode(int key)
{
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = key;
temp->next = NULL;
return temp;
}

/* Driver program to test above function*/
int main()
{

/* Create a loop for testing */

printf("Linked List after removing loop \n");
return 0;
}
```

## Java

```// Java program to detect and remove loop in linked list

static class Node {

int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

// Function that detects loop in the list
int detectAndRemoveLoop(Node node) {
Node slow = node, fast = node;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;

// If slow and fast meet at same point then loop is present
if (slow == fast) {
removeLoop(slow, node);
return 1;
}
}
return 0;
}

// Function to remove loop
void removeLoop(Node loop, Node head) {
Node ptr1 = loop;
Node ptr2 = loop;

// Count the number of nodes in loop
int k = 1, i;
while (ptr1.next != ptr2) {
ptr1 = ptr1.next;
k++;
}

// Fix one pointer to head

// And the other pointer to k nodes after head
for (i = 0; i < k; i++) {
ptr2 = ptr2.next;
}

/*  Move both pointers at the same pace,
they will meet at loop starting node */
while (ptr2 != ptr1) {
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}

// Get pointer to the last node
ptr2 = ptr2.next;
while (ptr2.next != ptr1) {
ptr2 = ptr2.next;
}

/* Set the next node of the loop ending node
to fix the loop */
ptr2.next = null;
}

// Function to print the linked list
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}

// Driver program to test above functions
public static void main(String[] args) {

// Creating a loop for testing
System.out.println("Linked List after removing loop : ");
}
}

// This code has been contributed by Mayank Jaiswal

```

## Python

```
# Python program to detect and remove loop in linked list

# Node class
class Node:

# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def detectAndRemoveLoop(self):

while(slow_p and fast_p and fast_p.next):
slow_p = slow_p.next
fast_p = fast_p.next.next

# If slow_p and fast_p meet at some point then
# there is a loop
if slow_p == fast_p:
self.removeLoop(slow_p)

# Return 1 to indicate that loop is found
return 1

# Return 0 to indicate that there is no loop
return 0

# Function to remove loop
# loop_node --> pointer to one of the loop nodes
# head --> Pointer to the start node of the linked list
def removeLoop(self, loop_node):
ptr1 = loop_node
ptr2 = loop_node

# Count the number of nodes in loop
k = 1
while(ptr1.next != ptr2):
ptr1 = ptr1.next
k += 1

# Fix one pointer to head

# And the other pointer to k nodes after head
for i in range(k):
ptr2 = ptr2.next

# Move both pointers at the same place
# they will meet at loop starting node
while(ptr2 != ptr1):
ptr1 = ptr1.next
ptr2 = ptr2.next

# Get pointer to the last node
ptr2 = ptr2.next
while(ptr2.next != ptr1):
ptr2 = ptr2.next

# Set the next node of the loop ending node
# to fix the loop
ptr2.next = None

# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)

def printList(self):
while(temp):
print temp.data,
temp = temp.next

# Driver program
llist.push(10)
llist.push(4)
llist.push(15)
llist.push(20)
llist.push(50)

# Create a loop for testing

llist.detectAndRemoveLoop()

print "Linked List after removing loop"
llist.printList()

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

Output:
```Linked List after removing loop
50 20 15 4 10 ```

Method 3 (Optimized Method 2: Without Counting Nodes in Loop)
We do not need to count number of nodes in Loop. After detecting the loop, if we start slow pointer from head and move both slow and fast pointers at same speed until fast don’t meet, they would meet at the beginning of the loop.

How does this work?
Let slow and fast meet at some point after Floyd’s Cycle finding algorithm. Below diagram shows the situation when cycle is found.

We can conclude below from above diagram

```
Distance traveled by fast pointer = 2 * (Distance traveled
by slow pointer)

(m + n*x + k) = 2*(m + n*y + k)

Note that before meeting the point shown above, fast
was moving at twice speed.

x -->  Number of complete cyclic rounds made by
fast pointer before they meet first time

y -->  Number of complete cyclic rounds made by
slow pointer before they meet first time

```

From above equation, we can conclude below

```    m + k = (x-2y)*n

Which means m+k is a multiple of n. ```

So if we start moving both pointers again at same speed such that one pointer (say slow) begins from head node of linked list and other pointer (say fast) begins from meeting point. When slow pointer reaches beginning of linked list (has made m steps). Fast pointer would have made also moved m steps as they are now moving same pace. Since m+k is a multiple of n and fast starts from k, they would meet at the beginning. Can they meet before also? No because slow pointer enters the cycle first time after m steps.

## C++

```// C++ program to detect and remove loop
#include<bits/stdc++.h>
using namespace std;

struct Node
{
int key;
struct Node *next;
};

Node *newNode(int key)
{
Node *temp = new Node;
temp->key = key;
temp->next = NULL;
return temp;
}

// A utility function to print a linked list
{
{
cout << head->key << " ";
}
cout << endl;
}

// Function to detect and remove loop
// in a linked list that may contain loop
{
// If list is empty or has only one node
// without loop
return;

// Move slow and fast 1 and 2 steps
slow = slow->next;
fast = fast->next->next;

// Search for loop using slow and
// fast pointers
while (fast && fast->next)
{
if (slow == fast)
break;
slow = slow->next;
fast = fast->next->next;
}

/* If loop exists */
if (slow == fast)
{
while (slow->next != fast->next)
{
slow = slow->next;
fast = fast->next;
}

/* since fast->next is the looping point */
fast->next = NULL; /* remove loop */
}
}

/* Driver program to test above function*/
int main()
{

/* Create a loop for testing */

printf("Linked List after removing loop \n");

return 0;
}
```

## Java

```// Java program to detect and remove loop in linked list

static class Node {

int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

// Function that detects loop in the list
void detectAndRemoveLoop(Node node) {

// If list is empty or has only one node
// without loop
if (node == null || node.next == null)
return;

Node slow = node, fast = node;

// Move slow and fast 1 and 2 steps
slow = slow.next;
fast = fast.next.next;

// Search for loop using slow and fast pointers
while (fast != null && fast.next != null) {
if (slow == fast)
break;

slow = slow.next;
fast = fast.next.next;
}

/* If loop exists */
if (slow == fast) {
slow = node;
while (slow.next != fast.next) {
slow = slow.next;
fast = fast.next;
}

/* since fast->next is the looping point */
fast.next = null; /* remove loop */
}
}

// Function to print the linked list
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}

// Driver program to test above functions
public static void main(String[] args) {

// Creating a loop for testing
System.out.println("Linked List after removing loop : ");
}
}

// This code has been contributed by Mayank Jaiswal

```

## Python

```
# Python program to detect and remove loop

# Node class
class Node:

# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)

def detectAndRemoveLoop(self):

return
return

# Move slow and fast 1 and 2 steps respectively
slow = slow.next
fast = fast.next.next

# Search for loop using slow and fast pointers
while (fast is not None):
if fast.next is None:
break
if slow == fast :
break
slow = slow.next
fast = fast.next.next

# if loop exists
if slow == fast :
while (slow.next != fast.next):
slow = slow.next
fast = fast.next

# Sinc fast.next is the looping point
fast.next = None # Remove loop

def printList(self):
while(temp):
print temp.data,
temp = temp.next

# Driver program

#Create a loop for testing

llist.detectAndRemoveLoop()

print "Linked List after removing looop"
llist.printList()

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

Output:
```Linked List after removing loop
50 20 15 4 10 ```

Thanks to Gaurav Ahirwar for suggesting above solution.

Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.5 Average Difficulty : 3.5/5.0
Based on 332 vote(s)