# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
# if not head or not head.next:
# return None
# slow=head
# fast=head.next
# while slow!=fast:
# if not fast or not fast.next:
# return None
# slow=slow.next
# fast=fast.next.next
# start=head
# while start!=slow:
# start=start.next
# slow=slow.next
# return slow
if not head or not head.next:
return None
dic={}
cur=head
while cur:
if dic.get(cur):
return cur
dic[cur]=cur.next
cur=cur.next
return
超时就用hashmap