题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

时间限制:1秒;空间限制:32768K;本题知识点:链表

解题思路

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        # write code here
        p = ListNode(1)
        new = p
        while pHead1!=None and pHead2!=None:
            if pHead1.val < pHead2.val:
                p.next = pHead1
                pHead1 = pHead1.next
            else:
                p.next = pHead2
                pHead2 = pHead2.next
            p = p.next   #记得更新p
        if pHead1!=None: #注意不要写成while
            p.next = pHead1
        if pHead2!=None:
            p.next = pHead2
        return new.next

 

相关文章:

  • 2022-02-07
  • 2022-12-23
  • 2021-08-20
  • 2021-05-18
  • 2021-06-19
  • 2021-10-20
猜你喜欢
  • 2021-07-06
  • 2021-12-12
  • 2021-11-22
  • 2021-12-22
  • 2021-09-21
  • 2021-08-10
  • 2022-01-08
相关资源
相似解决方案