-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathDetectLoop.java
38 lines (32 loc) · 1 KB
/
DetectLoop.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
package java1.algorithms.linkedlist;
public class DetectLoop {
private static boolean hasLoop(Node head) {
Node slowPtr = head;
Node fastPtr = head;
while(fastPtr != null && fastPtr.next != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next.next;
if(slowPtr == fastPtr) return true;
}
return false;
}
public static void main(String[] args) {
Node headNode = new Node(10);
Node node2 = new Node(20);
Node node3 = new Node(30);
Node node4 = new Node(40);
Node node5 = new Node(50);
Node node6 = new Node(60);
Node node7 = new Node(70);
Node node8 = new Node(80);
headNode.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
node4.setNext(node5);
node5.setNext(node6);
node6.setNext(node7);
node7.setNext(node8);
node8.setNext(headNode);
System.out.println(hasLoop(headNode));
}
}