题目描述:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
思路:
递归方法,直接代码的,没啥好说的额
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def Merge(self, pHead1, pHead2):
if not pHead1:
return pHead2
if not pHead2:
return pHead1
if pHead1.val < pHead2.val:
pre = pHead1
pre.next = self.Merge(pHead1.next, pHead2)
else:
pre = pHead2
pre.next = self.Merge(pHead1, pHead2.next)
return pre