题目地址:


Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

这道题比较简单,有多种解法。

python递归解法如下:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution:def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:if l1 is None:return l2elif l2 is None:return l1elif l1.val < l2.val:l1.next = self.mergeTwoLists(l1.next, l2)return l1else:l2.next = self.mergeTwoLists(l1, l2.next)return l2

Python迭代解法:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution:def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:prehead = ListNode(-1)prev = preheadwhile l1 and l2:if l1.val <= l2.val:prev.next = l1l1 = l1.nextelse:prev.next = l2l2 = l2.next            prev = prev.next# exactly one of l1 and l2 can be non-null at this point, so connect# the non-null list to the end of the merged list.prev.next = l1 if l1 is not None else l2return prehead.next

LeetCode 21. Merge Two Sorted Lists--合并2个有序列表--python递归,迭代解法相关推荐

  1. 合并k个有序链表 python_[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  2. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  3. LeetCode in Python-21. Merge Two Sorted Lists 合并两个有序链表

    Merge Two Sorted Lists 合并两个有序链表 题目 解法1. 解法2. 出处 题目 解法1. # Definition for singly-linked list. # class ...

  4. Leetcode 21:Merge Two Sorted Lists(golang实现合并两条已经排序的链表)

    21.Merge Two Sorted Lists 题目链接:题目链接 Merge two sorted linked lists and return it as a new list. The n ...

  5. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  6. LeetCode 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. leetcode 21 Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. [LeetCode] 21. Merge Two Sorted Lists ☆

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. [swift] LeetCode 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

最新文章

  1. java基础国庆作业_java程序设计国庆作业
  2. linux常用命令:top 命令
  3. 在哪能找到陌生人聊骚_如何说服陌生人帮助您找到工作
  4. 200行代码实现视频人物实时去除
  5. showModalDialog 使用详解
  6. c语言用switch字母判断星期几,c语言程序,输入年月日,判断这一天是这一年的第几天,同时判断这一天是星期几。(用switch语句)...
  7. Ajax Beta 2.0 中 AtlasToolKit Library 控件 Accordion 后台添加
  8. 比较版本号大小c语言,比较两个版本字符串version1和version2
  9. Kafka从上手到实践 - 实践真知:Kafka Java Consumer | 凌云时刻
  10. [INet] WebSocket 数据收发的详细过程
  11. 打印机连接电脑,USB无法识别,怎么办?
  12. Maya2018安装报错(错误代码1612)
  13. html页面计算圆的周长和面积,计算圆的周长和面积之间的差-JavaScript
  14. CSP-J 2021 小熊的果篮
  15. 浙江大学数据结构第一讲:1.1什么是数据结构
  16. 初学Python——协程
  17. 公司算法面试笔试题目集锦,个人整理,不断更新中
  18. Wireshark抓包实例诊断TCP连接问题
  19. 中心对称图形——平行四边形·复习整理
  20. 软件工程——编码、测试、维护

热门文章

  1. python大神教程_大神python教程415集全套,拿走不谢
  2. Science综述:人类与微生物共进退
  3. 下次诺贝尔奖会是他吗?肠道微生物组领域开创者Jeffrey Gordon
  4. 微生物相关网络构建教程:MENA, LSA, SparCC和CoNet
  5. python使用matplotlib可视化:设置坐标轴的范围、设置主次坐标轴刻度、坐标轴刻度显示样式、坐标轴刻度数颜色、小数点位数、坐标轴刻度网格线、线条类型、数据点形状标签、文本字体、颜色、大小等
  6. python使用matplotlib可视化跨年数值指标中位数变化率、使用pct_change函数计算变化率、年环比变化率(pct_change function)
  7. R语言编写自定义函数使用Wilcoxon符号秩检验(Wilcoxon signed rank)实现多分组非参数成对检验(pairwise)、并使用p.adjust函数调整概率值
  8. R语言ggplot2可视化:ggplot2可视化分组散点图并使用geom_smooth函数在散点图图中为不同的散点簇添加对应的回归曲线
  9. R语言伪相关性分析(Spurious Correlation)、相关关系不是因果关系:以缅因州离婚率数据集为例
  10. R语言构建xgboost模型:使用xgboost构建泊松回归(poisson regression)模型