Detect and Remove Loop in a Linked List in C++

In this article, we will discuss how to detect and remove Loop in a Linked List in C++ with different methods.

Create a function called detectAndRemoveLoop() that verifies whether a given Linked List has a loop. After that, it removes the Loop and returns true if it does. It returns false if there isn't a loop in the List. A linked list with a loop is depicted in the diagram below. The List below must be changed to 1->2->3->4->5->NULL using the detectAndRemoveLoop() method.

Write a C function to detect a loop in a linked list.

• We must first identify the Loop before attempting to remove it. Loops can be found using the methods discussed in the section above. All we have to do to end the Loop is obtain a pointer to the Loop's final node. Consider the node in the diagram above with the value 5. Once we know which node is the final, we may make the next node in this chain NULL to end the Loop.
• We may quickly apply the hashing or visited node approaches to obtain the pointer to the last node. The concept is straightforward: 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. The slow and fast pointers in Floyd's algorithm come together at a loop node. This loop node can be used to get rid of the cycle. When Floyd's technique detects loops, two distinct approaches exist to break the Loop.

Method 1: (Check each one separately)

Floyd's Cycle detection process is known to come to an end when the fast and slow pointers collide at the same location. Additionally, we are aware that this common point is one of the loop nodes (2, 3, 4, or 5 in the image above). Put this location in a pointer variable, like ptr2. After that, starting at the head of the Linked List, determine if each node is reachable from ptr2 by checking each one individually. When a node in a linked list is reachable, we can obtain the pointer to the preceding node of that node, indicating that this node represents the beginning of the Loop.

Output:

```Linked List after removing Loop
50 20 15 4 10
```

Method 2: (Better Solution)

1. This technique also requires Floyd's Cycle detection
2. Get the pointer to a loop node by utilizing Floyd's Cycle detection technique to find a loop.
3. Count how many nodes are in the Loop. Let k be the count.
4. One pointer should be fixed to the head and the other to a kth node from the head.
5. The pointers will collide at the Loop's starting node if we move them both at the same speed.
6. Obtain a pointer to the Loop's final node and make the next one NULL.

Program:

Let us take an example to illustrate how to detect and remove a loop in a linked list in C++.

Output:

Complexity Analysis:

Time Complexity: O(N), Where N is the number of nodes in the tree

Auxiliary Space: O(1)

Method 3: (Without Counting Nodes in Loop)

The number of nodes in the Loop does not need to be counted. 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 this work?

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

We can conclude from the above diagram.

From the above equation, we can conclude below

As a result, if we start moving both pointers again at the same speed, one pointer (let's call it slow) will start from the head node of the linked List while the other (let's call it fast) will start from the meeting point. The fast pointer would have also gone m steps because they are now going at the same speed when the slow pointer reaches the beginning of the Loop (having made m steps). They would collide at the beginning because the fast pointer starts from k, and m+k is a multiple of n.

Program:

Output:

Complexity Analysis:

Time Complexity: O(N), Where N is the number of nodes in the tree

Auxiliary Space: O(1)

We can check to see if an element already exists in the map by hashing the addresses of the linked list nodes in an unordered map. We must set the last node's next pointer to NULL because if it exists, we have reached a node that already exists by a cycle.

Program:

Output:

Complexity Analysis:

Time Complexity: O(N), Where N is the number of nodes in the tree.

Auxiliary Space: O(N), Where N is the number of nodes in the tree (due to hashing).