## Find loop starting point in a circular linked list

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
DEFINITION
A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
input: A -> B -> C -> D -> E -> C [the same C as earlier]
output: C

My initial thoughts:
Initialize with two pointers to the head, the slow pointer and fast pointer. Each time, slow pointer moves one step while the fast pointer moves two steps. If they meet in the some point, then that node must be the starting point of the loop.

My initial codes:

	public static LLNode findLoopBeginningPoint(

fastPointer = fastPointer.getNext();
fastPointer = fastPointer.getNext();

while (slowPointer != fastPointer) {
slowPointer = slowPointer.getNext();
fastPointer = fastPointer.getNext();
fastPointer = fastPointer.getNext();
}

return slowPointer;
}

The testing input yields out ‘E’ instead of the expected result ‘C’. Walk through the code:

fastPointer: ‘C’; slowPointer: ‘A’
fastPointer: ‘E’; slowPointer: ‘B’
fastPointer: ‘D’; slowPointer: ‘C’
fastPointer: ‘C’; slowPointer: ‘D’
fastPointer: ‘E’; slowPointer: ‘E’

The catch point of this algorithm is that when the fast pointer meets the slow pointer, the distance between their meeting point and the starting point of the loop, is equal to the distance between the head of the list and the starting point of the loop.
Let us denote the length of non-loop list as $m$, i.e., the distance between the head of the list to the starting point of the loop is $m$. Let’s also have the size of the loop as $L$. When the two pointer meets, let us denote the distance between the meeting point of the starting of the loop as $d$. So, at the time of their meet, the fast pointer would have travelled $m+nL-d$ and the slow pointer only travelled $m+L-d$. Therefore we have $m+nL-d=2(m+L-d) \Leftrightarrow d\equiv m \pmod L$.
Therefore, when the two pointer meets, we can move one pointer to the head, and then move them one step at each time, when they meet again, they will meet at the starting pointer of the loop.
public static LLNode<Character> findLoopBeginningPoint(

do {
slowPointer = slowPointer.getNext();
fastPointer = fastPointer.getNext().getNext();
} while (slowPointer != fastPointer);

if (slowPointer == null)
return null;

while (slowPointer != fastPointer) {
slowPointer = slowPointer.getNext();
fastPointer = fastPointer.getNext();
}

return slowPointer;
}

If the linked list has no loop, the highlighted line will raise an null-pointer exception under some circumstances.
Solution
public static LLNode<Character> FindBeginning(LLNode<Character> head) {

// find meeting point
while (n2.getNext() != null) {
n1 = n1.getNext();
n2 = n2.getNext().getNext();
if (n1 == n2)
break;
}

// Error check - there is no meeting point, and therefore no loop
if (n2.getNext() == null)
return null;

/*
* Move n1 to Head. Keep n2 at Meeting Point. Each are k steps from the
* Loop Start. If they move at the same pace, they must meet at Loop
* Start.
*/
while (n1 != n2) {
n1 = n1.getNext();
n2 = n2.getNext();
}

return n2;
}

__ATA.cmd.push(function() {
__ATA.initVideoSlot('atatags-370373-5b27fe534d9be', {
sectionId: '370373',
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-26942-5b27fe534d9f1',  {
collapseEmpty: 'before',
sectionId: '26942',
width: 300,
height: 250
});
});

__ATA.cmd.push(function() {
__ATA.initSlot('atatags-114160-5b27fe534d9f3',  {
collapseEmpty: 'before',
sectionId: '114160',
width: 300,
height: 250
});
});