Skip to content

Commit eae19b9

Browse files
committed
DaleStudy#225 Linked List Cycle
1 parent c74e278 commit eae19b9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

linked-list-cycle/forest000014.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)