We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a6dc8d3 commit d37425bCopy full SHA for d37425b
LinkedListCycle.java
@@ -0,0 +1,36 @@
1
+/**
2
+ *
3
+ */
4
+package leetcode;
5
+
6
+import java.util.HashSet;
7
+import java.util.Set;
8
9
10
+ * @author hzlinqien
11
12
13
14
+public class LinkedListCycle {
15
16
+ public boolean hasCycle(ListNode head) {
17
+ Set<ListNode> nodeMap = new HashSet<>();
18
+ for (ListNode temp = head; temp != null; temp = temp.next) {
19
+ if (nodeMap.contains(temp)) {
20
+ return true;
21
+ }
22
+ nodeMap.add(temp);
23
24
+ return false;
25
26
27
+ class ListNode {
28
+ int val;
29
+ ListNode next;
30
31
+ ListNode(int x) {
32
+ val = x;
33
+ next = null;
34
35
36
+}
0 commit comments