Nonetype has no attribute for .next


  • 0
    A

    Hi I have a very weird question here.
    I had my answer right as following:

    def hasCycle(self, head): 
        slow = head
        fast = head
        while ((fast.next != None) and (fast != None)):
            slow = slow.next
            fast = fast.next.next
            if (fast ==slow):
                return True
        return False
    

    but once I tried to switch the order of

    fast.next != None

    and

    fast.next != None

    It shows Nonetype has no attribute for .next

    Does anyone knows why?


  • 0
    D

    if head is None,when program run to the while,first i would check whether the fast have the next attribute,but the head is None,so the program directly throw the error do not run the next !


Log in to reply
 

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