【LeetCode每天一题】Merge Two Sorted Lists(合并两个排序链表)
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
解决思路:最简单的办法是,直接将所有元素图取出来放入内存中,然后进行排序,将排序好的结果重新构造链表然后返回。但是这样做时间复杂度较高,O((m+n)log(m+n))(m, n分别为l1 和l2 的长度), 空间复杂度为O(m+n)。
另一种办法是,依此进行比较大小,然后将小的节点插入申请链表节点尾部,循环遍历,直到其中一个链表便遍历完毕。时间复杂度为O(m+n), 空间复杂度为O(m+n)。
步骤图如下:

class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 or not l2: # 其中一个空节点,直接返回非空的节点
return l1 if l1 else l2
res_node = p = ListNode() # 哨兵节点
while l1 and l2:
if l1.val < l2.val:
p.next = l1
l1 = l1.next
else:
p.next = l2
l2 = l2.next
p = p.next
p.next = l1 or l2 # 将非空的节点加到尾部
return res_node.next # 返回排序后的节点
【LeetCode每天一题】Merge Two Sorted Lists(合并两个排序链表)的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- [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 ...
- [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 ...
- [Leetcode] 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 ...
- LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- 021 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 ...
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
随机推荐
- ssh如何连接vmware的centos 6虚拟机
先安装虚拟机,我使用的是WMware 11.1.3 build-3206955+Centos7 其中出现一个问题是,我需要在虚拟机上安装软件,需要上网,虚拟机有几种网络连接方式: 暂时了解的是,“仅主 ...
- Spring学习笔记--Spring简介
1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...
- 10.13 Django随笔
2018-10-13 14:20:59 越努力,越幸运! 永远不要高估自己! Django的渲染是在render()时候渲染的,然后把字符串传给浏览器 Django请求流程, 跨域 参考链接: htt ...
- Entity Framework Core的坑:Skip/Take放在Select之前造成Include的实体全表查询
今天将一个迁移至 ASP.NET Core 的项目放到一台 Linux 服务器上试运行.站点启动后,浏览器打开一个页面一直处于等待状态.接着奇怪的事情发生了,整个 Linux 服务器响应缓慢,ssh命 ...
- 对Entity Framework Core的一次误会:实体状态不跟踪
在 Entity Framework 中,当通过 EF 使用 LINQ 查询获取到一个实体(实际得到的是 EF 动态生成的实体类的代理类的实例)时,这个实体的状态默认是被跟踪的.所以,当你修改实体的某 ...
- 淘宝NPM镜像cnpm
# 安装cnpm命令 npm install -g cnpm --registry=https://registry.npm.taobao.org2.cnpm install
- pygme 安装
输入pip install pygame-1.9.3-cp36-cp36m-win32.whl ModuleNotFoundError: No module named 'requests' pip ...
- Python中给List添加元素的4种方法
https://blog.csdn.net/hanshanyeyu/article/details/78839266 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持 ...
- Python开发【笔记】:列表转字典
列表转字典 it = [1,2,3,4] print(dict(zip(it, it))) # {1: 1, 2: 2, 3: 3, 4: 4} it = iter(it) print(dict(zi ...
- Servlet (二)ServletContext
package cn.sasa.serv; import java.io.IOException; import javax.servlet.ServletContext; import javax. ...