Sharing my Python solution


  • 11
    C

    Same idea as many other posts, just the python version:

    def detectCycle(self, head):
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                break
        else:
            return None
        while head != slow:
            slow = slow.next
            head = head.next
        return head

  • 0
    C

    Nice code, thanks for the "while ... else" clause in Python!


Log in to reply
 

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