努力中的小白️ 2021-11-15 07:05 采纳率: 57.1%
浏览 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日

悬赏问题

  • ¥15 matlab数据降噪处理,提高数据的可信度,确保峰值信号的不损失?
  • ¥15 怎么看我在bios每次修改的日志
  • ¥15 python+mysql图书管理系统
  • ¥15 Questasim Error: (vcom-13)
  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据