Linked List Cycle II

Solution Code
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                break
        else:
            return None
        
        pointer = head
        while pointer != slow:
            pointer = pointer.next
            slow = slow.next
        return pointer

Time Complexity

O(N)

Space Complexity

O(1)