Maybe the current test cases needed to be completed

  • 0

    Hi there,
    when i tried to AC this question by Brent's cycle detection algorithm, i forgot to reset a steps counter. But this uncompleted solution also passed all test cases, so there could be a test case to cover this accidental situation.

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    # = None
    class Solution(object):
        def hasCycle(self, head):
            :type head: ListNode
            :rtype: bool
            if not head or not
                return False
            tortoise, hare = head,
            steps, stepsLimit = 0, 1
            while and tortoise is not hare:
                if steps == stepsLimit:
                    tortoise = hare
                    stepsLimit *= 2
                    # HERE I FORGOT TO RESET steps => steps = 0
                hare =
                steps += 1
            return tortoise is hare

Log in to reply

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