Linked List Cycle II LeetCode solution in Python
1# Definition for singly-linked list.2# class ListNode:3# def __init__(self, x):4# self.val = x5# self.next = None678class Solution:9def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:10if not head or not head.next:11return None12slow = fast = entry = head1314while fast and fast.next:15slow = slow.next16fast = fast.next.next1718if slow == fast:19while slow != entry:20slow = slow.next21entry = entry.next22return entry23return None