剑指offer-合并两个排序链表16

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
 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

 

转载于:https://www.cnblogs.com/zhaiyansheng/p/10414921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值