如何在Python中合并两个链表



我有一个单链接列表:

x1->x2->无

其中x是此列表的元素,称为节点。每个节点都存储一些数据。

我试过:

class Node:
def __init__(self,value:int):
self.value = value
self.next = None 

# here I write a second method like merge

def merge(list1:Node, list2:Node) -> Node:
head_first = list1
while head_first.next:
head_first = head_first.next
head_first.next = list2
return list1

n1=Node(1)
n2=Node(2)

n1.next=n2

我想加入两个列表,但争论的状态不应该改变。

但它不起作用。我得到一个错误:

AttributeError:"int"对象没有属性"next"。

以下是的答案

def merge(list1:Node, list2:Node) -> Node:
head = list1
while head.next:
head = head.next
head.next = list2
return list1

答案是这样的吗。

x = {'a': 1, 'b': 2}
y = {'b': 10, 'c': 11}
z = x.update(y)
print(z)

最新更新