# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return
slow = fast = head
while slow and fast and fast.next :
slow = slow.next
fast = fast.next.next
if slow is fast:
break
if fast is None or fast.next is None: #表明没有环
return
tmp = head
while tmp is not slow:
tmp = tmp.next
slow = slow.next
return slow