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 c74e278 commit eae19b9Copy full SHA for eae19b9
linked-list-cycle/forest000014.java
@@ -0,0 +1,29 @@
1
+/**
2
+ # Time Complexity : O(n)
3
+ # Space Complexity: O(1)
4
+
5
+ * Definition for singly-linked list.
6
+ * class ListNode {
7
+ * int val;
8
+ * ListNode next;
9
+ * ListNode(int x) {
10
+ * val = x;
11
+ * next = null;
12
+ * }
13
14
+ */
15
+public class Solution {
16
+ private final int VISITED = -999999;
17
+ public boolean hasCycle(ListNode head) {
18
+ while (head != null) {
19
+ if (head.val == VISITED) {
20
+ return true;
21
+ }
22
23
+ head.val = VISITED;
24
+ head = head.next;
25
26
27
+ return false;
28
29
+}
0 commit comments