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?