-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
LoopDetection.java
67 lines (60 loc) · 2.12 KB
/
LoopDetection.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.ctci.linkedlists;
/**
* @author rampatra
* @since 2019-02-03
*/
public class LoopDetection {
/**
* Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.
* DEFINITION
* Circular linked list: 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
* <p>
* See {@link com.rampatra.linkedlists.DetectAndRemoveLoop} for a slightly more complex problem.
*
* @param head the starting node of the linked list
* @return the {@code Node} where the loop starts, {@code null} otherwise.
*/
private static Node findLoopStartNode(Node head) {
Node slow = head;
Node fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
/* Error check - 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 Loop Start. */
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
/* You can return either as now both point to the start of the loop */
return fast;
}
public static void main(String[] args) {
/*
* 1 -> 2 -> 3 -> 4 -> 5 -> 6
* ^ |
* |_________|
*/
Node l1 = new Node(1);
l1.next = new Node(2);
l1.next.next = new Node(3);
l1.next.next.next = new Node(4);
l1.next.next.next.next = new Node(5);
l1.next.next.next.next.next = new Node(6);
l1.next.next.next.next.next.next = l1.next.next.next;
System.out.println(findLoopStartNode(l1).val);
}
}