【发布时间】:2020-10-21 19:24:20
【问题描述】:
我写了一个函数来在 Python 中合并两个排序的链表。这是代码:-
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next is not None:
last_node = last_node.next
last_node.next = new_node
def print(self):
cur_node = self.head
while cur_node.next is not None:
print(cur_node.data, "-> ", end='')
cur_node = cur_node.next
print(cur_node.data)
def merge(l1, l2):
cur_node1 = l1.head
cur_node2 = l2.head
l3 = LinkedList()
while cur_node1.next is not None or cur_node2.next is not None:
if cur_node1.data > cur_node2.data:
l3.append(cur_node2.data)
cur_node2 = cur_node2.next
else:
l3.append(cur_node1.data)
cur_node1 = cur_node1.next
if cur_node1.next is None:
l3.append(cur_node1.data)
while cur_node2.next is not None:
l3.append(cur_node2.data)
cur_node2 = cur_node2.next
l3.append(cur_node2.data)
elif cur_node2.next is None:
l3.append(cur_node2.data)
while cur_node1.next is not None:
l3.append(cur_node1.data)
cur_node1 = cur_node1.next
l3.append(cur_node1.data)
return l3
ll1 = LinkedList()
ll2 = LinkedList()
ll1.append(12)
ll1.append(45)
ll1.append(69)
ll1.append(70)
ll2.append(1)
ll2.append(2)
ll2.append(99)
ll2.append(100)
ll3 = merge(ll1, ll2)
ll3.print()
但是在运行代码后我得到了这个错误 File "C:/Users/_/PycharmProjects/DAA/SinglyLinkedList.py", line 179, in <module> ll3 = merge(ll1, ll2) File "C:/Users/_/PycharmProjects/DAA/SinglyLinkedList.py", line 143, in merge while (cur_node1.next is not None or cur_node1 is not None) or (cur_node2.next is not None or cur_node2 is not None): AttributeError: 'NoneType' object has no attribute 'next'
这里发生了什么?我不明白。我尝试在合并函数的 while 循环中运行没有 or 语句的代码。它工作得很好。所以显然问题出在while语句中。有人可以帮忙吗?
【问题讨论】:
-
我认为您希望该检查为
cur_node1 and cur_node2,因为您在循环中执行的第一件事是访问cur_node1.data和cur_node2.data。不确定这是否足以使合并正常工作;这可能表明您没有考虑极端情况。 -
考虑
cur_node1 is None的情况。您希望cur_node1.next is not None or cur_node1 is not None评估成功吗?为什么?怎么样? -
是的,这绝对不够——您的初始 while 循环将离开一个列表或另一个列表的末尾,因此之后的代码需要能够处理其中至少一个这两个节点是
None。 -
我将 cur_node1 设为第一个链表的指针,将 cur_node2 设为第二个链表的指针。现在我比较了我在两个链表中遍历的特定节点中的数据。如果某些特定数据较小,我将其附加到新创建的列表 l3 中。现在,如果我们在比较时到达一个链表的末尾,我们应该退出 while 循环。 cur_node1 永远不会是 None,因为当 cur_node1 是指向 list1 中最后一个节点的指针时,cur_node1.next 会变为 None,因此它将在下一次迭代中跳出 while 循环。 or 运算符导致问题。
-
Lmao,没关系。我只是将
while循环更改为while True:并将条件放入循环中。如果满足条件(cur_node1.next is not None or cur_node2.next is not None),我就跳出循环。现在效果很好。
标签: python oop data-structures linked-list