zoukankan      html  css  js  c++  java
  • 合并两个排序的链表

     1 # -*- coding:utf-8 -*-
     2 # class ListNode:
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.next = None
     6 class Solution:
     7     # 返回合并后列表
     8     def Merge(self, pHead1, pHead2):
     9         # write code here
    10         if pHead1 is None:  #首先考虑极端的特殊情况
    11             return pHead2
    12         if pHead2 is None:
    13             return pHead1
    14         pMergeHead = None
    15         if pHead1.val<pHead2.val:
    16             pMergeHead = pHead1
    17             pMergeHead.next = self.Merge(pHead1.next,pHead2) #这里递归的时候方法的调用依然要用类实例.方法,而类中的self就是指类实例
    18         else:
    19             pMergeHead = pHead2
    20             pMergeHead.next = self.Merge(pHead2.next,pHead1) 
    21         return pMergeHead   #递归的输出是两个链表排序合并后的列表的指针(就是这个链表)
  • 相关阅读:
    蚂蚁
    N的阶乘
    最小公倍数LCM
    最大公约数GCD
    Truck History(卡车历史)
    亲戚
    [SDOI2011]打地鼠
    连续自然数和
    P4250 [SCOI2015]小凸想跑步
    P4048 【[JSOI2010]冷冻波】
  • 原文地址:https://www.cnblogs.com/Henry-ZHAO/p/12725302.html
Copyright © 2011-2022 走看看