Very clear python solution where you don't need to worry about the corner case


  • 7
    H
    class Solution:
        # @param head, a ListNode
        # @return a boolean
        def hasCycle(self, head):
            try:
                fast = head.next.next
                slow = head.next
                
                while fast != slow:
                    fast = fast.next.next
                    slow = slow.next
                
                return True
            except:
                return False

  • 0
    S

    Very elegant!


Log in to reply
 

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