Python solutions (dictionary and two pointers).

  • 0
    # Utilize O(n) extra space
    def hasCycle(self, head):
        dic = {}
        while head:
            if head in dic:
                return True
            dic[head] = 0
            head =
        return False
    # follow up: classic two poiters method
    def hasCycle1(self, head):
        fast = slow = head
        while fast and
            fast = 
            slow =
            if slow is fast:
                return True
        return False

  • 0

    why not just use set?

Log in to reply

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