-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCycleList.java
More file actions
51 lines (47 loc) · 974 Bytes
/
Copy pathCycleList.java
File metadata and controls
51 lines (47 loc) · 974 Bytes
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
public class CycleList {
private Node head;
public CycleList() {
this.head = new Node("head");
}
public Node head() {
return head;
}
public void appendToTail(Node node) {
Node current = head;
while (current.next() != null) {
current = current.next;
}
current.setNext(node);
}
public boolean isLoop() {
Node fast = head;
Node slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
return true;
}
}
return false;
}
public static class Node {
private Node next;
private String data;
public Node(String data) {
this.data = data;
}
public String data() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node next() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}