【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:Input:
[
1->4->5,
1->3->4,
2->6
]
Output: 1->1->2->3->4->4->5->6 思路
这道题最简单的方法就是我们将K个链表中的所有数据都存进数组中,然后对数据进行排序。排序完毕之后在依此对数组中的数据进行链表的构造。时间复杂度为O(n log n),n 是链表元素的总和。
第二种思路是我们我们可以将列表中两个链表两个链表进行合并,直到最后合并完只剩下一条链表,就是排序之后的链表。时间复杂度为O(Nlog k)(N是总的元素数),空间复杂度为O(1)。
第二种图示
第一种方法的解决代码
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
self.nodes = []
head = point = ListNode(0) # 设置哨兵节点
for l in lists: # 循环遍历列表中每一个链表,将元素添加进数组。
while l: # 将当前链表中的元素进行添加
self.nodes.append(l.val)
l = l.next
for x in sorted(self.nodes): # 将排序之后的数组中的元素,构造链表。
point.next = ListNode(x)
point = point.next
return head.next
第二种方法的解决代码
class Solution(object):
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
amount = len(lists) # 计算出K的数值
interval = 1 # 间距
while interval < amount: # 先从间距1开始合并,
for i in range(0, amount - interval, interval * 2):
lists[i] = self.merge2Lists(lists[i], lists[i + interval]) # 使用两个链表的合并函数进行合并。
interval *= 2 # 当上一次间距合并完之后,将间距扩大为2倍。
return lists[0] if amount > 0 else lists # 当列表链表数大于0时,返回第一个链表(排序好的链表)。 def merge2Lists(self, l1, l2): # 两个链表进行合并成一个链表。
head = point = ListNode(0) # 设置哨兵节点
while l1 and l2:
if l1.val <= l2.val:
point.next = l1
l1 = l1.next
else:
point.next = l2
l2 = l1
l1 = point.next.next
point = point.next
if not l1: # 其中一个链表不为空,就将其添加到后面。
point.next=l2
else:
point.next=l1
return head.next
【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [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 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. 这 ...
- [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】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
随机推荐
- mui---取消掉默认加载框
我们在进行打开页面新页面的时候,在APP中会在中间有一个加载框,考虑到用户体验,要取消掉,具体方法是,对openWindow进行配置: 具体参考:http://dev.dcloud.net.cn/mu ...
- B - Space Ant
The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists trace ...
- A - Fire Net
Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...
- java封装实现Excel建表读写操作
对 Excel 进行读写操作是生产环境下常见的业务,网上搜索的实现方式都是基于POI和JXL第三方框架,但都不是很全面.小编由于这两天刚好需要用到,于是就参考手写了一个封装操作工具,基本涵盖了Exce ...
- 解决css设置背景透明,文字不透明
设置元素的透明度: -moz-opacity:0.8; /*在Firefox中设置元素透明度 filter: alpha(opacity=80); /*ie使用滤镜设置透明 但是当我们对一个标 ...
- ARM Linux Oops使用小结(转)
出现Oops消息的大部分错误时因为对NULL指针取值或者因为用了其他不正确的指针值. Oops如何产生的解释如下: 由于处理器使用的地址几乎都是虚拟地址,这些地址通过一个被称为“页表”的结构被 ...
- ELK之从6.3.1升级至6.6.2
需要把原6.3.1版本升级为6.6.2版本 1,官网下载rpm包 2,升级elasticsearch和kibana rpm -U elasticsearch-6.6.2.rpm rpm -U kiba ...
- 线段树 || BZOJ 1112: [POI2008]砖块Klo
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1112 题解: 希望有连续K柱的高度是一样的,就先把1~K的数扔进线段树(线段树的下标就是数值 ...
- maven工程之pom模板(hadoop、hive、hbase)
以下配置文件涵盖了hadoop.hive.hbase开发支持库的配置. 仅需针对maven工程pom.xml文件做相应更改就可以自动生成hadoop开发支持库. <properties> ...
- .net WebService的使用
1. WebService可单独作为一个网站,不限平台的被调用. 2. 打开VS,选择新建 3. [WebMethod] 方法上面有这个说明,则表示此方法可被外部调用. 我们添加4个方法:加.减.乘. ...
第一种方法的解决代码