题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
1 class Solution: 2 # 返回合并后列表 3 def Merge(self, pHead1, pHead2): 4 # write code here 5 if pHead1==None: 6 return pHead2 7 if pHead2==None: 8 return pHead1 9 head=ListNode(-1) 10 head.next=None 11 root=head 12 while pHead1 is not None and pHead2 is not None: 13 if pHead1.val<pHead2.val: 14 head.next=pHead1 15 head=pHead1 16 pHead1=pHead1.next 17 else: 18 head.next=pHead2 19 head=pHead2 20 pHead2=pHead2.next 21 if pHead1 is None: 22 head.next=pHead2 23 else: 24 head.next=pHead1 25 return root.next