forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindBeginning.java
36 lines (31 loc) · 923 Bytes
/
FindBeginning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Given a circular linked list, implement an algorithm which returns
//the node at the beginning of the loop
public class FindBeginning {
LinkedListNode findBeginning(LinkedListNode head) {
LinkedListNode slow = head;
LinkedListNode fast = head;
/* find meeting point. This will be LOOP_SIZE - k
* steps int othe linked list */
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(fast == slow) {
break;
}
}
/* error checking - no meeting point, and therefore no loop */
if(fast == null || fast.next == null) {
return null;
}
/* move slow to head. Keep fast at meeting point. Each are k
* steps from the loop start. If they move at the same pace,
* they must meet at the loop start */
slow = head;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
/* both now point to the start of the loop */
return fast;
}
}