What's wrong with my python solution? It works on my python IDLE.


  • 1
    T

    I got this:

    Runtime Error Message: Line 15: AttributeError: ListNode instance has no attribute 'len'
    Last executed input: No intersection: {1,3,5,7,9,11,13,15,17,19,21}, {2}

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param two ListNodes
        # @return the intersected ListNode
        def getIntersectionNode(self, headA, headB):
            if (headA ==None) or (headB ==None):
                return None
            
            nodeL = []
            for i in range (min([len(headA)]+[len(headB)])):
                if headA[::-1][i] ==headB[::-1][i]:
                    nodeL.append(headA[::-1][i])
                else:
                    break
                
            nodeL.reverse()
        
            if nodeL == []:
                return None
            else:
                return nodeL

  • 0
    M

    OK - Here is the reason you are getting the Attribute error .

    you cannot use len() because , this is not the inbuilt python list.

    what you see , when the test fails is something like this = {1,3,5,7,9,11,13,15,17,19,21} .

    This is not the inbuilt list like [1,3,5,7,9,11,13,15,17,19,21]. Thats the reason you cannot use len() function.

    Each element in that list -is of the structure mentioned in the problem statement.

     Definition for singly-linked list.
     class ListNode:
         def __init__(self, x):
             self.val = x
             self.next = None
    

Log in to reply
 

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