Linked List Cycle II

Problem Id: 142 Difficulty: Medium


Intuition

Solution


class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        fast = slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                count = 1
                fast = fast.next
                while head != slow:
                    head = head.next
                    slow = slow.next
                return slow
        return None