努力中的小白️ 2021-11-15 07:05 采纳率: 53.3%
浏览 13

请问为什么leetcode没报错但是sublime报错

请问大家 为什么会报错if l1.val < l2.val:
AttributeError: 'list' object has no attribute 'val'


```python
class ListNode:
    def __init__(self, val=0, nextNode=None):
        self.val = val
        self.next = nextNode


    def mergeTwoLists(self,l1, l2):
        # Check if either of the lists is null
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        # Choose head which is smaller of the two lists
        if l1.val < l2.val:
            temp = head = ListNode(l1.val)
            l1 = l1.next
        else:
            temp = head = ListNode(l2.val)
            l2 = l2.next
        # Loop until any of the list becomes null
        while l1 is not None and l2 is not None:
            if l1.val < l2.val:
                temp.next = ListNode(l1.val)
                l1 = l1.next
            else:
                temp.next = ListNode(l2.val)
                l2 = l2.next
            temp = temp.next
        # Add all the nodes in l1, if remaining
        while l1 is not None:
            temp.next = ListNode(l1.val)
            l1 = l1.next
            temp = temp.next
        # Add all the nodes in l2, if remaining
        while l2 is not None:
            temp.next = ListNode(l2.val)
            l2 = l2.next
            temp = temp.next
        return head
l1 = [1,2,3]
l2 = [2,3,4]
hh = ListNode()
hh.mergeTwoLists(l1,l2)


```

  • 写回答

1条回答 默认 最新

  • qza2468 2021-11-15 08:28
    关注

    16至21行每一个l1都改成l1[0],l2同理

    还有很多类似的要改

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 11月15日