我正在尝试将2个已排序的链接列表合并到单个已排序的链接列表中。
href
但我收到的错误是:
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data =data
self.next = None
class LinkedList:
def mergeNode(self, second_list):
llist4 = LinkedList()
first_node = self.head
second_node = second_list.head
while (first_node!=None or second_node != None):
if (first_node.data > second_node.data):
llist4.push(first_node)
prev = first_node
first_node = first_node.next
elif (first_node.data < second_node.data):
llist4.push(second_node)
prev = second_node
second_node = second_node.next
else:
llist4.push(first_node)
llist4.push(second_node)
prev = second_node
second_node = second_node.next
first_node = first_node.next
return llist4
if __name__ == '__main__':
llist = LinkedList()
llist2 = LinkedList()
llist3 = LinkedList()
llist.push(15)
llist.push(10)
llist.push(5)
llist2.push(12)
llist2.push(3)
llist2.push(2)
llist3 = llist.mergeNode(llist2)
llist3.printList()
我知道我可能在我的代码中使用None值,因为我无法弄清楚原因。有人请帮我找出问题吗?
答案 0 :(得分:0)
first_node!=None or second_node != None
并不保证两个值均为None
。
first_node = None
second_node = "something"
print first_node!=None or second_node != None # True
使用适当的条件:
while first_node is not None and second_node is not None:
do_something()