头属性中的Python中合并两个链接列表错误

时间:2019-02-04 13:38:50

标签: python-3.x

  

merge_linked_lists中文件“ merge.py”的第55行,node1 = lst1.head(AttributeError:“ Node”对象没有属性“ head”)   在merge_linked_lists node1 = lst1.head中的文件“ merge.py”,第55行(AttributeError:“ Node”对象没有属性“ head”)   在merge_linked_lists node1 = lst1.head中的文件“ merge.py”,第55行(AttributeError:“ Node”对象没有属性“ head”)

class Node:
    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next = next_node

    def __str__(self):
        return str(self.data)


class LinkedList:
    def __init__(self):
        self.length = 0
        self.head = None

    def print_list(self):
        node = self.head
        while node is not None:
            print(node, end=' ')
            node = node.next
        print('')

    def add_at_head(self, node):
        node.next = self.head
        self.head = node
        self.length += 1

    def remove_node_after(self, node):
        if node.next is not None:
            temp = node.next
            node.next = node.next.next
            temp.next = None
            self.length -= 1

    def remove_first_node(self):
        if self.head is None:
            return
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1

    def print_backward(self):
        def print_nodes_backward(node):
            if node.next is not None:
                print_nodes_backward(node.next)
            if node is not None:
                print(node, end=' ')

        if self.head is not None:
            print_nodes_backward(self.head)
        print('')

def merge_linked_lists(lst1, lst2):

    node1=lst1.head
    node2=lst2.head
    temp=None
    if(node1 is None):
        return node2

    if(node2 is None):
        return node1

    if(node1.data <= node2.data):
        temp=node1
        node1.next=merge_linked_lists(node1.next,node2)

    else:
        temp=node2
        node2.next=merge_linked_lists(node2.next,node1)
    return temp

pass

lst11=LinkedList()
lst11.add_at_head(Node(1))
lst11.add_at_head(Node(2))
lst11.add_at_head(Node(3))

lst22=LinkedList()
lst22.add_at_head(Node(4))
lst22.add_at_head(Node(5))
lst22.add_at_head(Node(6))

merge_linked_lists(lst11,lst22)
  

如何使用此方法合并列表   它的写入(AttributeError:“ Node”对象没有属性“ head”)

0 个答案:

没有答案