【问题标题】:Receiving error when merging two linked lists合并两个链表时收到错误
【发布时间】:2020-06-18 14:28:31
【问题描述】:

我正在尝试在 Python 中合并两个链表,但每次我收到错误:类型对象 'List' 显然具有合并功能时没有属性 'merge'。


示例输入:
8 11 20 24 50
5 9 10 30 33 40 45
样本输出:
5 8 9 10 11 20 24 30 33 40 45 50


class Node: 
    def __init__ (self, data): 
       self.data = data 
       self.next = None
class List: 
    def __init__(self): 
       self.head = None
    def append(self, new_data): 
       new_node = Node(new_data) 
       if self.head is None: 
            self.head = new_node 
            return
       last = self.head 
       while last.next: 
            last = last.next
       last.next = new_node 
    def merge(head1, head2): 
       temp = None
       if head1 is None: 
          return head2 
       if head2 is None: 
          return head1 
       if head1.data <= head2.data: 
          temp = head1 
          temp.next = merge(head1.next, head2) 
       else: 
          temp = head2
          temp.next = merge(head1, head2.next)
       return temp 

接受输入:

inp = [int(i) for i in input().split(" ")]
inp2 = [int(i) for i in input().split(" ")]
l = List()
l2 = List()
for i in inp:
    l.append(i)
for i in inp2:
    l2.append(i)
rez = List.merge(l, l2)
print(rez)

感谢您的宝贵时间!

【问题讨论】:

  • 也许你离开了 @static_method 装饰器? merge 当前是实例方法,而不是类方法(尽管您将 self 保留为第一个参数)
  • merge() 的参数应该是Lists 还是Nodes?你的称呼不一致。
  • 我看不出这段代码是如何产生这个错误的。在您的真实代码中,merge() 函数是否缩进在 List 类下面?
  • 当我运行你的代码时,我得到一个不同的错误 btw AttributeError: 'List' object has no attribute 'data' 这是正确的,因为 .data 在你的 Node 类上,但 if head1.data &lt;= head2.data: 你正在混淆节点和列表。我建议您开始使用类型提示来使您的代码更清晰。如果这样做,您会发现 List 是一个错误的名称选择,因为它已被 typing 模块采用
  • 请编辑问题以包含完整的错误回溯消息。

标签: python list


【解决方案1】:

如果你想让你的方法在类而不是对象上是可调用的,你应该把它设为static:

@staticmethod
def merge(head1, head2): 
    # etc...

您还可以在混淆 ListNode 时使用类型提示,使代码更清晰(包括对您自己):

from typing import Any, Optional

class Node:
    def __init__(self, data: Any):
        self.data = data
        self.next: Optional[Node] = None  # might be Optional["Node"] since it inside it's own definition, I'm not sure check the mypy docs


class LinkedList:
    def __init__(self, head: Optional[Node] = None):
        self.head = head

    def append(self, new_data: Any) -> None:
        new_node = Node(new_data)
        if self.head is None:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node

    @staticmethod
    def merge(list1: "LinkedList", list2: "LinkedList") -> "LinkedList":
        if list1.head is None:
            return list2
        if list2.head is None:
            return list1
        if list1.head.data <= list2.head.data:
            head = list1.head
            list1 = LinkedList(list1.head.next)
        else:
            head = list2.head
            list2 = LinkedList(list2.head.next)
        temp = LinkedList(head)
        temp.head.next = LinkedList.merge(list1, list2).head
        return temp 

测试它:

inp = [8, 11, 20, 24, 50]
inp2 = [5, 9, 10, 30, 33, 40, 45]
l = LinkedList()
l2 = LinkedList()
for i in inp:
    l.append(i)
for i in inp2:
    l2.append(i)
rez = LinkedList.merge(l, l2)

temp = rez.head
print(temp.data)
while temp.next:
    temp = temp.next
    print(temp.data)

【讨论】:

  • 这也是我的第一个想法,但问题比这更深。使用您的解决方案运行代码会报错:AttributeError: 'List' object has no attribute 'data'
  • 同意 - 但这不是问题
  • 感谢您的宝贵时间!会考虑:)
【解决方案2】:

您对头部和列表对象类型感到困惑。你有时会用列表调用它,然后用头递归调用它,为你修复它:

class Node: 
    def __init__ (self, data): 
       self.data = data 
       self.next = None
class List: 
    def __init__(self): 
       self.head = None
    def append(self, new_data): 
       new_node = Node(new_data) 
       if self.head is None: 
            self.head = new_node 
            return
       last = self.head 
       while last.next: 
            last = last.next
       last.next = new_node 
    def merge(head1, head2):
       list_given = type(head1) == List
       if list_given:
           head1 = head1.head
           head2 = head2.head
       temp = None
       if head1 is None: 
          return head2 
       if head2 is None: 
          return head1 
       if head1.data <= head2.data: 
          temp = head1 
          temp.next = List.merge(head1.next, head2) 
       else: 
          temp = head2
          temp.next = List.merge(head1, head2.next)

       if list_given:
           new_list = List()
           new_list.head = temp
           return new_list
       else:
           return temp 


inp = [int(i) for i in input().split(" ")]
inp2 = [int(i) for i in input().split(" ")]

l = List()
l2 = List()
for i in inp:
    l.append(i)
for i in inp2:
    l2.append(i)
rez = List.merge(l, l2)
print(rez)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    相关资源
    最近更新 更多