-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain141.java
More file actions
39 lines (33 loc) · 773 Bytes
/
Main141.java
File metadata and controls
39 lines (33 loc) · 773 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
package HOT100;
import java.util.HashSet;
import java.util.Set;
// Hash方法
public class Main141 {
private Set<ListNode> set = new HashSet<>();
public boolean hasCycle(ListNode head) {
if(head==null){
return false;
}
while (head!=null){
if(!set.contains(head)){
set.add(head);
}else {
return true;
}
head = head.next;
}
return false;
}
}
// 快慢指针
class Main141_1{
public boolean hasCycle(ListNode head){
ListNode s = head, f = head;
while(f!=null && f.next!=null){
s = s.next;
f = f.next.next;
if(s == f) return true;
}
return false;
}
}