Why my code is wrong?


  • 0

    The following code is my newest code that I has been modified it correctly!
    class Solution(object):
    def isNextTrue(self, x):
    """
    :type x:ListNode
    """
    if(x is not None):
    return x.next is not None

    def isNNextTrue(self, x):
        """
        :type x:ListNode
        """
        if(x is not None and x.next is not None):
            return x.next.next is not None
        
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        rstr = None
        if(head is None):
            return rstr
        elif(head.next is None):
            return rstr
        slow = head.next
        fast = head    
        if(self.isNNextTrue(fast)):
            fast = fast.next.next
            
        while(slow is not fast):
            if(self.isNextTrue(slow)):
                    slow = slow.next
            else:
                    return rstr
            if(self.isNNextTrue(fast)):
                fast = fast.next.next
            else:
                return rstr
        
        if(fast is slow):
            fast = head
        else:
            return rstr
        while(fast is not slow):
                fast = fast.next
                slow = slow.next              
        return fast

  • 0

    if there is no cycle,return None object rather than "null",please!


Log in to reply
 

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