Skip to content

Commit 9f41dc7

Browse files
committed
Time: 38 ms (91.76%), Space: 19 MB (49.14%) - LeetHub
1 parent f68415b commit 9f41dc7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def hasCycle(self, head: Optional[ListNode]) -> bool:
9+
if not head or not head.next:
10+
return False
11+
12+
slow = head
13+
fast = head.next
14+
15+
while slow != fast:
16+
if not fast or not fast.next:
17+
return False
18+
slow = slow.next
19+
fast = fast.next.next
20+
21+
return True

0 commit comments

Comments
 (0)