构造两个带有表头节点的有序单链表La 、Lb. 编写程序实现将La,Lb 合并成一个有序单链表Lc
时间: 2024-05-11 20:15:47 浏览: 91
好的,我可以为您提供代码,以下是实现的Python代码:
```
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
def merge_sorted_lists(la, lb):
dummy_node = Node()
tail = dummy_node
while True:
if la is None:
tail.next = lb
break
if lb is None:
tail.next = la
break
if la.data <= lb.data:
tail.next = la
la = la.next
else:
tail.next = lb
lb = lb.next
tail = tail.next
return dummy_node.next
# Example usage:
la = Node()
la.next = Node(1)
la.next.next = Node(3)
lb = Node()
lb.next = Node(2)
lb.next.next = Node(4)
lc = merge_sorted_lists(la.next, lb.next)
while lc:
print(lc.data)
lc = lc.next
```
希望能够帮到您,如果您还有其他问题需要解答,请随时提出。
阅读全文