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. Spring学习笔记--Spring IOC

    沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...

  2. meat http-equiv 属性详解

    转自 http://kinglyhum.iteye.com/blog/827807 http-equiv 属性提供了 content 属性的信息/值的 HTTP 头. http-equiv 属性可用于 ...

  3. MFC 应用程序中使用管道代码示意

    STARTUPINFO sinf = {0}; PROCESS_INFORMATION pinf = {0}; SECURITY_ATTRIBUTES sa = {0}; HANDLE hPipeOR ...

  4. ERP项目实施记录03

    今天继续测试,3周了,终于弄到采购进货.

  5. 怎么关闭win10防火墙

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "Disable ...

  6. JS 通过字符串取得对应对象

    //对Array的扩展,查找所有满足条件的元素 Array.prototype.findAll = function (match) { var tmp = []; for (var i = 0; i ...

  7. F#周报2018年第48期

    新闻 F#2018年圣诞日历 Mac上的Visual Studio 2017新版本7.7 Rider 2018.3将引入远程调试功能 Visual Studio 2017新版本15.9.3 视频及幻灯 ...

  8. c 语言笔记 数组1

    1.数组可以有多维数组.c99支持动态数组,c11和c99之前不再支持. 2.数组 初始化一个后,后面的自动初始化为0,如果不初始化,都是垃圾值. 3.数组初始化 可以指定  ss[10]={0,2, ...

  9. Codeforces 1137D - Cooperative Game - [交互题+思维题]

    题目链接:https://codeforces.com/contest/1137/problem/D 题意: 交互题. 给定如下一个有向图: 现在十个人各有一枚棋子(编号 $0 \sim 9$),在不 ...

  10. [dpdk] SDK编译配置

    前言: dpdk-16.07.2 与 内核Linux-3.10.0-514.6.1.el7.x86_64 编译的时候有个关于kni的错误 CC [M] /root/src/thirdparty/dpd ...