【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. 这 ...
随机推荐
- celery 原理理解
这里有一篇写的不错的:http://www.jianshu.com/p/1840035cb510 自己的“格式化”后的内容备忘下: 我们总在说c10k的问题, 也做了不少优化, 然后优化总是不够的. ...
- db2 reorg(转)
DB2 reorg RUNSTATS: db2 connect to rmdb11 user rmadmin using rmadmin 对所有用户表执行runstats(reorgchk加updat ...
- okvis代码解读
okvis_timing模块 提供时间转换的功能函数 okvis_util模块 包含 assert_macros.hpp 该文件包含一些有用的断言宏. source_file_pos.hpp 该文件 ...
- Dom4j中getStringValue()和getText()用法的区别
这两个方法都是获取文本的,区别是: getText()-----获取当前节点的文本内容,如果当前节点下是一个element元素,那返回的就是null. getStringValue------获取当前 ...
- MyBati__mapper 中取值(#{} 或${}) 以及 parameterType为(基本类型 或复杂类型)
参考资料: MyBatis学习笔记(三)——parameterType为基本类型时的使用方法 MyBatis的传入参数parameterType类型 1. MyBatis的传入参数parameterT ...
- day3_字典
一.说明 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: dict = {key1:value1,key2 ...
- 树和二叉树->相互转化
文字描述 由上篇关于树和二叉树的存储结构知,树和二叉树都可以采用二叉链表作为存储结构.也就是说,给定一颗树,可以找到惟一的一颗二叉树与之对应,从物理结构来看,它们的二叉链表是相同的,只是解释不同而已. ...
- ECharts图形库
ECharts图形库百度的项目,图形的创建也比较简单,直接引用Javascript即可 1,引入<script src="{{ url_for("static",f ...
- NOIP观光公交
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- python pip install 报错TypeError: unsupported operand type(s) for -=: 'Retry' and 'int' Command "python setup.py egg_info" failed with error code 1 in
pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl ...