Given a linked list of integers, which may contain a cycle, remove the cycle if present.

For example, the following linked list has a cycle in it:

Cycle in Linked List

 
The solution should remove the cycle from the list. The output list should be as follows:

Linked List with cycle removed

Practice this problem

This post provides an overview of some available alternatives to accomplish this – using hashing and Floyd’s cycle detection algorithm.

 
Related Post:

Detect cycle in a linked list (Floyd’s Cycle Detection Algorithm)

1. Using Hashing

A simple solution is to use hashing. The idea is to traverse the given linked list and insert each encountered node into a hash set. If the node is already present in the HashSet, that means it is seen before and points to the first node in the cycle. To break the cycle, set the next pointer of the previous node to null.

Following is the implementation of the above idea in C++, Java, and Python. The code uses two pointers: curr and prev, where curr is the main pointer running down the list, and prev trails it.

C++


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> nullptr

Java


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> null

Python


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> None

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the total number of nodes in the linked list.

2. Using Floyd’s Cycle Detection Algorithm

As seen in the previous post, Floyd’s cycle detection algorithm maintains two pointers where the first pointer moves at twice the speed of the second pointer. If both pointers meet at some point, a cycle is found in the list.

First, the idea is to check if a cycle is present in a linked list using Floyd’s cycle detection algorithm and get a pointer to the loop node where fast and slow pointers meet. If a cycle is found, remove it using that loop node. The trick is to find the first node in the linked list that is reachable from the loop node. This node would be the starting node of the loop in the linked list. To break the chain, just set the next pointer of its previous node to null.

 
Following is the implementation in C++, Java, and Python based on the above idea:

C++


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> nullptr

Java


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> null

Python


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> None

The time complexity of the above approach is O(n2), where n is the total number of nodes in the linked list.

 
We can easily improve the time complexity to O(n). The idea is to get a pointer to the loop node using Floyd’s cycle detection algorithm and count the total number of nodes in the loop, say k, using that loop node. Then take two pointers – the first pointer pointing to the head of the linked list and the second pointer pointing to the k'th node from the head. If we move both pointers simultaneously, they will eventually meet at the loop starting node. To break the chain, set the next pointer of the last node of the loop to null.

Following is the implementation of the above approach in C++, Java, and Python:

C++


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> nullptr

Java


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> null

Python


Download  Run Code

Output:

1 —> 2 —> 3 —> 4 —> 5 —> None