-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCycle.java
More file actions
112 lines (98 loc) · 2.75 KB
/
FindCycle.java
File metadata and controls
112 lines (98 loc) · 2.75 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Implements finding cycle in a linked list algorithm.
*/
package linkedlist;
import java.util.HashSet;
class FindCycle {
/**
* Checks if list has a cycle by changing it's values.
* @param list The list to search cycle in.
* @return True if there is a cycle, false otherwise.
*/
public static boolean hasCycleWithChanges(LinkedList<Integer> list) {
if (list == null) {
return false;
}
if (list.value == -1) {
return true;
}
list.value = -1;
return hasCycleWithChanges(list.next);
}
/**
* Checks if list has a cycle by comparing next link to
* the previous elements.
* @param list The list to search cycle in.
* @param current The current element, by default the same as list.
* @return True if there is a cycle, false otherwise.
*/
public static boolean hasCycle1(LinkedList list, LinkedList current) {
if (current == null || current.next == null) {
return false;
}
if (current == current.next) {
return true;
}
LinkedList cur = list;
while (cur != current) {
if (current.next == cur) {
return true;
}
cur = cur.next;
}
return hasCycle1(list, current.next);
}
/**
* Checks if list has a cycle by storing all elements in a hashset.
* @param list The list to search cycle in.
* @return True if there is a cycle, false otherwise.
*/
public static boolean hasCycle2(LinkedList list) {
if (list == null) {
return false;
}
HashSet<LinkedList> seen = new HashSet<LinkedList>();
if (seen.contains(list)) {
return true;
}
seen.add(list);
return hasCycle2(list.next, seen);
}
/**
* Checks if list has a cycle by storing all elements in a hashset;
* private function which helps function hasCycle2 with one parameter.
* @param list The list to search cycle in.
* @param seen The HashSet with all items observed.
* @return True if there is a cycle, false otherwise.
*/
private static boolean hasCycle2(LinkedList list, HashSet<LinkedList> seen) {
if (list == null) {
return false;
}
if (seen.contains(list)) {
return true;
}
seen.add(list);
return hasCycle2(list.next, seen);
}
/**
* Checks if list has a cycle by having two pointers.
* @param list The list to search cycle in.
* @return True if there is a cycle, false otherwise.
*/
public static boolean hasCycle3(LinkedList list) {
if (list == null || list.next == null) {
return false;
}
LinkedList slow = list.next;
LinkedList fast = list.next.next;
while (slow != fast) {
if (slow.next == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}