題目描述:
輸入兩個單調(diào)遞增的鏈表,輸出兩個鏈表合成后的鏈表温算,當然我們需要合成后的鏈表滿足單調(diào)不減規(guī)則。
解題思路:遞歸調(diào)用
# -*- 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
l1 = pHead1
l2 = pHead2
if l1 == None and l2 == None:
return None
if l1==None and l2!=None:
return l2
if l1!=None and l2==None:
return l1
if l1!=None and l2!=None:
if l1.val>=l2.val:
head = l2
head.next = self.Merge(l1,l2.next)
else:
head = l1
head.next = self.Merge(l1.next,l2)
return head