题解 | #合并两个排序的链表#
合并两个排序的链表
http://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回合并后列表 def Merge(self, pHead1, pHead2): #递归法,用三个指针,不断判断MergedHead.next是哪一个 if pHead1==None: return pHead2 elif pHead2==None: return pHead1 MergedHead=None if pHead1.val<pHead2.val: MergedHead=pHead1 MergedHead.next=self.Merge(pHead1.next, pHead2) else: MergedHead=pHead2 MergedHead.next=self.Merge(pHead1, pHead2.next) return MergedHead