How can I detect that whether a singly linked-list has loop or not??
If it has loop then how to find the point of origination of the loop i.e. the node from which the loop has started.
How can I detect that whether a singly linked-list has loop or not?? If
Share
You can detect it by simply running two pointers through the list, this process is known as the tortoise and hare algorithm after the fable of the same name:
headisnull). If so, no cycle exists, so stop now.tortoiseon the first nodehead, and the second pointerhareon the second nodehead.next.hareisnull(which may be already true in a one-element list), advancingtortoiseby one andhareby two in each iteration. The hare is guaranteed to reach the end first (if there is an end) since it started ahead and runs faster.Consider the following loop which starts at
3:Starting
tortoiseat 1 andhareat 2, they take on the following values:Because they become equal at
(6,6), and sincehareshould always be beyondtortoisein a non-looping list, it means you’ve discovered a cycle.The pseudo-code will go something like this:
The time complexity for this algorithm is
O(n)since the number of nodes visited (by tortoise and hare) is proportional to the number of nodes.Once you know a node within the loop, there’s also an
O(n)guaranteed method to find the start of the loop.Let’s return to the original position after you’ve found an element somewhere in the loop but you’re not sure where the start of the loop is.
This is the process to follow:
hareand setsizeto1.hareandtortoiseare different, continue to advancehare, increasingsizeeach time. This eventually gives the size of the cycle, six in this case.sizeis1, that means you must already be at the start of the cycle (in a cycle of size one, there is only one possible node that can be in the cycle so it must be the first one). In this case, you simply returnhareas the start, and skip the rest of the steps below.hareandtortoiseto the first element of the list and advancehareexactlysizetimes (to the7in this case). This gives two pointers that are different by exactly the size of the cycle.hareandtortoiseare different, advance them both together (with the hare running at a more sedate pace, the same speed as the tortoise – I guess it’s tired from its first run). Since they will remain exactlysizeelements apart from each other at all times,tortoisewill reach the start of the cycle at exactly the same time asharereturns to the start of the cycle.You can see that with the following walkthrough:
Hence
3is the start point of the cycle and, since both those operations (the cycle detection and cycle start discovery) areO(n)and performed sequentially, the whole thing taken together is alsoO(n).If you want a more formal proof that this works, you can examine the following resources:
If you’re simply after support for the method (not formal proof), you can run the following Python 3 program which evaluates its workability for a large number of sizes (how many elements in the cycle) and lead-ins (elements before the cycle start).
You’ll find it always finds a point where the two pointers meet: