合并两个排序的链表 发表于 2018-06-01 | 分类于 interview | 次 ###题目 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 解题思路递归实现。 代码实现1234567891011121314151617181920212223# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Nonep = Noneclass Solution: # 返回合并后列表:递归 def Merge(self, pHead1, pHead2): # write code here if pHead1 is None and pHead2 is None: return None if pHead1 is None: return pHead2 if pHead2 is None: return pHead1 if pHead2.val < pHead1.val: p = pHead2 p.next = self.Merge(pHead1, pHead2.next) else: p = pHead1 p.next = self.Merge(pHead1.next, pHead2) return p 打赏 微信支付 支付宝