-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path141.py
More file actions
27 lines (21 loc) · 741 Bytes
/
141.py
File metadata and controls
27 lines (21 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head:
return False
slow = head
fast = head
# The fast and slow approach move the fast pointer ahead the slow
# so if there is a cycle the fast pointer will eventually find the slow pointer
# that give us the confirmation of cycle into the list
while slow and fast and fast.next is not None:
slow = slow.next
fast = fast.next.next
if fast == slow:
return True
return False