美文网首页
合并两个排序链表

合并两个排序链表

作者: momo1023 | 来源:发表于2019-03-27 16:12 被阅读0次
    # -*- 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
            pre = ListNode(0)
            head = pre
            while pHead1 and pHead2:
                if pHead1.val > pHead2.val:
                    head.next = pHead2
                    pHead2 = pHead2.next
                else:
                    head.next = pHead1
                    pHead1 = pHead1.next
                head = head.next
            if pHead1:
                head.next = pHead1
            if pHead2:
                head.next = pHead2
            return pre.next
    

    相关文章

      网友评论

          本文标题:合并两个排序链表

          本文链接:https://www.haomeiwen.com/subject/ksazvqtx.html