forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_141.java
37 lines (32 loc) · 966 Bytes
/
_141.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import java.util.HashSet;
import java.util.Set;
public class _141 {
public static class Solution1 {
public boolean hasCycle(ListNode head) {
Set<ListNode> set = new HashSet();
while (head != null) {
if (!set.add(head)) {
return true;
}
head = head.next;
}
return false;
}
}
public static class Solution2 {
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}
}
}