Skip to content

Commit 72068b1

Browse files
committed
Time: 0 ms (100%), Space: 44.2 MB (88.56%) - LeetHub
1 parent ce6d852 commit 72068b1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public boolean hasCycle(ListNode head) {
14+
if(head == null) return false;
15+
ListNode slow = head;
16+
ListNode fast = head.next;
17+
while(fast != null && fast.next != null){
18+
if(slow == fast) return true;
19+
slow = slow.next;
20+
fast = fast.next.next;
21+
}
22+
return false;
23+
}
24+
}

0 commit comments

Comments
 (0)