合并两个排序的链表(python)

题目描述

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

2019-12-08 08:58:27

猜你喜欢

转载自www.cnblogs.com/NPC-assange/p/12004589.html
今日推荐