Friday, April 26, 2024
HomeBusinessHow do you detect a loop in a linked list and remove...

How do you detect a loop in a linked list and remove it?

Do you know how to detect any loop in the linked list or remove it?

If yes, you are on the right blog post!

This is among the most crucial concepts of a coding and a programming interview. 

A linked list is touted as the collection of data elements that are not provided by the physical placement in a required memory. 

There may be times when you have to detect or remove loop in linked list. In that case, you have to follow certain approaches.

Here, in this regard, we have created this blog post! 

Learn about the knits and grits of these approaches to fill your knowledge base! 

Understanding of loop in the linked list 

A linked list is touted to contain a loop that will be having the last node which is pointing towards the pointing at the other node rather than it pointing towards other nodes that are NULL. 

Remove a loop in the linked list signifies the making of the last node point that is null and is not in the other nodes of a given list. 

How to find the loop in a linked list?

In order to find the loop in a linked list, you can take the help of below-mentioned approaches:

Floyd Cycle Algorithm Approach 

In this approach, the first step would be to detect if a given linked list has a loop or not. The quickest and easiest way to do this is to use the Floyd Cycle algorithm. 

In this approach, you can use a two pointer which one moves slower than the other to traverse a list. The other one is the hare-tortoise algorithm which gives the reference of a child story about the race of a tortoise and hare. 

Follow these steps in the algorithm:

  • Both pointers would begin by pointing at a first node 
  • The two pointers will start to traverse the lit by prt1 as one node at any given time and prt2 as the nodes of other time 
  • Since the ptr2 moves generally faster than the ptr1, you can soon reach at the last of the list

There will be two possible scenarios in this case:

In the first one, there will not be any loop in a list. Ptr 2 would point towards the NULL to move the two nodes. Ptr2 will become null even in case of several nodes. If there is the even or odd number, ptr 2 would still say null.

The second scenario would be the presence of the loop in a linked list. The pointers in this case would not reach NULL. they enter a loop to cross over there. During traversing a list, they will meet any type of particular node. 

As any one pointer finds that loop, it will start traversing on that of varying speeds. Hence, instead of finding any null, you will get the ptr 1 = ptr 2. 

The execution may stop as the output will be shown as the present. 

The two pointers would meet at the particular node in the loop. In the first case, we will stop as there is no loop. While, in the case of the second scenario, we need to move to another situation i.e removing the loop. 

Remove the loop in a linked list 

To remove the loop in a linked list, we can move a detected loop in a case that the last element would point towards the NULL.

The steps to follow in this case are:

  • Ptr 1 and ptr2 would finally point towards the given node in a loop. First, we will find the last element of the loop to make it NULL
  • Leave the ptr 2 at its current location while you make the ptr1 towards the head of a loop 
  • Next, we can traverse at the two pointers in a given linked list. Instead of ptr2 moving faster than the ptr, both the pointers will move at the same pace that is for one node at a given time. The traversal will continue until any two pointers will meet at a given node. From this point, the loop will begin.
  • It is the time to remove a loop. We can point towards the null value of the ptr2. But remember that any next pointers would always meet at the start of a given loop. 
  • It is the time to remove a loop. Make ptr 2 as the null value. Remember that at the start of the next pointer, it will always meet at the start of a given loop. 

You may wonder why the prtr1 and ptr 2 will denote at the start of a loop. This is by using the loops. 

Since the ptr moves two nodes at a given time or at twice the speed of both ptr1 and ptr2. The distance between the given two pointers would be [ N -L] %L in any case. The ptr 2 will move across the entire loop in a linked list so as to cover the [ n-l] nodes.

The next distance between pointers would be [ N-L]%L plus 2. After that, the next distance would be [ N – L]%L plus 2. 

It means that it is easy for you to find the required distance between any two pointers using this formula from the beginning of a required loop. 

However, their execution part should be stopped when ptr 1 and the ptr2 will meet at the same nodes.

Additional Learning – first unique character in a string

Another useful concept in coding that you should know is the first unique character in a string. The unique characters are the ones that are non-repeating. 

A string is given to you. You have to find the non-repeating characters to get the required output in an ideal way. 

Wrapping Up 

To detect and remove a loop in a linked list, you can consider these approaches so that you would be better equipped with the knowledge of this concept.

Happy coding!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments