Recently, I have went through the algorithm for detecting, first node in the cycle as stated below:-
Note: This statement has been excerpted from site:-
http://javabypatel.blogspot.in/2015/12/detect-loop-in-linked-list.html
Now point is, how we will come to know the start node of a loop.
STEP 1: After finding the loop, both pointers are pointing to common node that is at meeting point
inside loop,
STEP 2: Now, move one pointer among them(say tortoise) to the head of list, keeping hare
pointer at the same position that is at meeting point inside loop.
STEP 3: Start moving both pointers one node at a time. The place they both will meet is the start
node of the loop/cycle.
Lets consider Case 2 in the following image:-
While applying all steps, in the case 2, I got tucked up in Step 3
"The following is the work outs for the Hare and Tortoise algorithm against the case 2."
Faster - 1 Slower - 1
Faster - 5 Slower - 20
Faster - 22 Slower - 5
Faster - 20 Slower - 18
Faster - 18 Slower - 22
Faster - 1 Slower - 1 - Both of the pointers meet here..
So, as per STEP 2, I am keeping the Faster Pointer as it is, and moving the Slow Pointer to 1 (which is the head of the list).
So, as per STEP 3, I am incrementing each pointer one by one.
Since, both of them are in "1", moving each pointer by one, will never end and it moves on and on.
Q1. May I kindly know, whether I got understood anything wrong here in STEP 3?
Q2. May I assume that, the node where the loop is ending, is the first node in the cycle?
-
See also this question. It has nice answers with pictures!Norman– Norman2016年04月18日 15:06:53 +00:00Commented Apr 18, 2016 at 15:06
1 Answer 1
You seem to have traced the given algorithm correctly until the very last step. At the very last step, though, I think you mistook the term the head of list to be the pointer to the first element of the list. Consequently, you may have inferred that the loop would never end as both pointers would move at the same speed and one would be ahead of the other by 1. Yet, I believe the head of the list was used to refer to the first element in the list, not the pointer to the first element.
By the way, I do not think your confusion is rooted in a mistake made by you, but rather in an ambiguity in the pseudocode you provided in your question. Even if you did not misinterpret the head of the list, you would execute Step 3 of the pseudocode once, and conclude that the pointers would meet at node with label 20. (i.e. the one after the first node of the list) So, I think Step 3 could use some clarification as the following.
STEP 3: While hare and tortoise are at different cells, move both pointers one node at a time.
Corrected like this, if a case like case 2 you provided is encountered, after moving one of the pointers to the beginning of the list, the condition to iterate will not be fulfilled, as the pointers have already met. (due to the start of cycle being at the beginning of the list)
Even though your original Q2 was quite unclear, let me answer to it based on your clarifications to it as comments to my answer.
In the link you provided, it is shown that the meeting point M the hare and the tortoise pointers meet after Step 1 of the pseudocode you provided has the exact distance to the start of the loop as the first element of the list has. (i.e. distances p and r in the diagram above are equal) Consequently, if one pointer is placed at the beginning of the list and the other remains at their meeting point, if both move by 1 as long as they are not at the same node of the list, yeah, you may assume where they meet will be the beginning of the loop in the list, if such a loop ever exists.
5 Comments
Explore related questions
See similar questions with these tags.