-
Notifications
You must be signed in to change notification settings - Fork 8
/
LinkedListCycle.java
65 lines (57 loc) · 1.64 KB
/
LinkedListCycle.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
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
package twopointers;
import java.util.HashSet;
import java.util.Set;
// Source : https://leetcode.com/problems/linked-list-cycle/
// Id : 141
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-05-29
// Topic : Two Pointers
// Level : Easy
// Other : Must
// Tips :
// Result : 100.00% 99.88%
public class LinkedListCycle {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null)
return false;
Set<ListNode> set = new HashSet<>();
ListNode node = head;
while (node.next != null) {
node = node.next;
if (set.contains(node))
return true;
else
set.add(node);
}
return false;
}
/**
* 快指针每次移动两格,慢指针每次移动一格
*
* 感觉上似乎会有"恰好"被跳过的可能,实际上不会
* 假设快慢都是移动一格,他们是相对静止的,现在快只是多移动一格,相当于快在一格一格的"追赶"慢指针
*
* @param head
* @return
*/
public boolean hasCycle1(ListNode head) {
// public boolean hasCycle(ListNode head) {
if (head == null || head.next == null)
return false;
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow)
return true;
}
return false;
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}