# 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:
slow = head
fast = head
meet = None
# Check if there is a cycle first
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
meet = slow
break
if not meet:
return None
p1 = head
p2 = meet
while p1 != p2:
p1 = p1.next
p2 = p2.next
return p1