Delete a loop in a single list

A loop may occur in a singly linked list (SLL).
To remove a loop in the list, we first need to detect the loop in SLL, and then delete the loop.

Can anyone tell me how to remove a loop in SLL with pseudo-code?
Can we do this using 3 pointers?
Is there an alternative for the task?

+4
source share
1 answer

There are many solutions you ask. One of the simplest but ineffective methods is to flip the list while maintaining the head of the node. If you go back to the head of the node, you know that there is a loop.

Another way to check is to create an array containing an int for each node in the list, each time you visit the node, increase its corresponding value in the array. Then all you have to do is check if there is more than one value in the array, and then compare it with where the additional iterations start. This method detects full loops and small loops. Hope this helps.

+1
source

All Articles