题目
合并两个有序链表
1–>2–>4,1–>3–>4
1–>1–>2–>3–>4–>4
思路
一次比较两个链表的每个元素,比较小的那个连到cur上,当前指针指向下一个。直到结束。
代码
def mergetowsortlink(link1,link2):
dummy = cur = Node()
l1 = link1.head.next
l2 = link2.head.next
while l1 is not None and l2 is not None:
if l1.value<l2.value:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
if l1 is not None:
cur.next = l1
else:
cur.next = l2
return dummy.next