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个有序链表)的更多相关文章

  1. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

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

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

  6. 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 ...

  7. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

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

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

  9. [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 ...

  10. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

随机推荐

  1. web初级开发的那些坑

    1.在使用js原生的XMLHttpRequest加载.xml文件时,老是不对,按照书上的写的没错,后来才发现是我的web.xml文件中阻止了.xml文件的加载. 2.有关于string解析成json数 ...

  2. day_6.5 py

    Wireshark的使用  2018-6-5 20:16:05 明天学 03

  3. Zookeeper中Session Timeout的那些事

    前言: RDS系统致力于MySQL数据的高可用,高可靠,高性能以及在线扩展功能,实现这些特性的主要逻辑功能都运行在管理服务器上,一旦管理服务器宕机,数据库的在线扩展功能/备份功能/故障恢复功能等都无从 ...

  4. html学习_表格、表单

    表格(table):是用来处理表格式数据的,不是用来布局的. table > tr(行标签)>  td(单元格标签) 1.表格注意事项: tr只能放置td标签,td里面可以放置任意元素. ...

  5. virtuanenv+flask

    1.virtualenv&flask 专门为特定项目创建一个目录和一个虚拟的Python 运行环境 # 1.安装 virtualenv$ pip3 install virtualenv #.创 ...

  6. PostgreSQL+PostGIS安装以及使用

    安装,参照:    https://www.cnblogs.com/ytwy/p/6817179.html 创建企业级地理文件数据库时报错," You must copy the lates ...

  7. Only the storage referenced by ptr is modified. No other storage locations are accessed by the call.

    free - C++ Reference http://www.cplusplus.com/reference/cstdlib/free/ Data races Only the storage re ...

  8. URL编码问题

    一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号. 比如,世界上有英文字母的网址"http://www.abc.com", 但是没有希腊字母的网址 ...

  9. git忽略UserInterfaceState.xcuserstate

    使用sourceTree,  忽略

  10. Python爬虫加速神器的小试

    大名鼎鼎的aiohttp,相信如果你学习Python或者爬虫的时候,肯定听说过这个东西.没听过也不要紧,今天看完文章,只要记住,aiohttp这个东西,在写爬虫的时候,很牛逼就行了. aiohttp ...