Fastest (80 ms) Python O(n) time O(1) space solution

  • 0

    Same hare-tortoise algorithm with slightly different implementation.

    class Solution(object):
            def detectCycle(self, head):
                if not head or not
                    return None
                slow,fast = head,
                while fast and
                    slow =
                    fast =
                    if slow == fast:
                        # there is a cycle
                        slow =
                        p = head
                        while p != slow:
                            p =
                            slow =
                        return p
                return None

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.