-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0141_LinkedListCycle.java
103 lines (99 loc) · 3.01 KB
/
_0141_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
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
package com.diguage.algorithm.leetcode;
import com.diguage.algorithm.util.ListNode;
import com.diguage.algorithm.util.ListNodeUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* = 141. Linked List Cycle
*
* https://leetcode.com/problems/linked-list-cycle/[Linked List Cycle - LeetCode]
*
* Given a linked list, determine if it has a cycle in it.
*
* To represent a cycle in the given linked list, we use an integer `pos` which represents the position (0-indexed) in the linked list where tail connects to. If `pos` is `-1`, then there is no cycle in the linked list.
*
* .Example 1:
* [source]
* ----
* Input: head = [3,2,0,-4], pos = 1
* Output: true
* Explanation: There is a cycle in the linked list, where tail connects to the second node.
* ----
*
* .Example 2:
* [source]
* ----
* Input: head = [1,2], pos = 0
* Output: true
* Explanation: There is a cycle in the linked list, where tail connects to the first node.
* ----
*
* .Example 3:
* [source]
* ----
* Input: head = [1], pos = -1
* Output: false
* Explanation: There is no cycle in the linked list.
* ----
*
* *Follow up:*
*
* Can you solve it using O(1) (i.e. constant) memory?
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-12 10:06
*/
public class _0141_LinkedListCycle {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Linked List Cycle.
*
* Memory Usage: 37.9 MB, less than 84.29% of Java online submissions for Linked List Cycle.
*/
public boolean hasCycle(ListNode head) {
if (Objects.isNull(head)) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (Objects.nonNull(fast)) {
if (slow == fast) {
return true;
}
slow = slow.next;
fast = fast.next;
if (Objects.nonNull(fast)) {
fast = fast.next;
}
}
return false;
}
/**
* Runtime: 5 ms, faster than 6.60% of Java online submissions for Linked List Cycle.
*
* Memory Usage: 38.3 MB, less than 82.14% of Java online submissions for Linked List Cycle.
*
* Copy from: https://leetcode.com/problems/linked-list-cycle/solution/[Linked List Cycle solution - LeetCode]
*/
public boolean hasCycleMap(ListNode head) {
Set<ListNode> nodes = new HashSet<>();
ListNode current = head;
while (Objects.nonNull(current)) {
if (nodes.contains(current)) {
return true;
} else {
nodes.add(current);
}
current = current.next;
}
return false;
}
public static void main(String[] args) {
_0141_LinkedListCycle solution = new _0141_LinkedListCycle();
ListNode l1 = ListNodeUtils.build(Arrays.asList(3, 2, 0, -4));
l1.next.next.next.next = l1.next;
boolean r1 = solution.hasCycle(l1);
System.out.println(r1);
}
}