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
    #         self.next = None
    
    class Solution(object):
        def hasCycle(self, head):
            """
            :type head: ListNode
            :rtype: bool
            """
            if not head or not head.next:
                return False
            tortoise, hare = head, head.next
            steps, stepsLimit = 0, 1
            while hare.next and tortoise is not hare:
                if steps == stepsLimit:
                    tortoise = hare
                    stepsLimit *= 2
                    # HERE I FORGOT TO RESET steps => steps = 0
                hare = hare.next
                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.