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 5ef6197 commit 60cf867Copy full SHA for 60cf867
src/LinkedListCycle.java
@@ -0,0 +1,26 @@
1
+import java.util.HashSet;
2
+import java.util.Set;
3
+
4
+public class LinkedListCycle {
5
+ static class ListNode {
6
+ int val;
7
+ ListNode next;
8
+ ListNode(int x) {
9
+ val = x;
10
+ next = null;
11
+ }
12
13
14
+ public boolean hasCycle(ListNode head) {
15
+ Set<ListNode> nodes = new HashSet<>();
16
+ while (head != null) {
17
+ if (nodes.contains(head)) {
18
+ return true;
19
+ } else {
20
+ nodes.add(head);
21
22
+ head = head.next;
23
24
+ return false;
25
26
+}
0 commit comments