题目

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

分析

合并k个有序链表。

我们从数据结构的链表章节学会了如何合并两个链表,针对此题,一个简单的解法便是遍历k次,两个链表合并,合并后的结果与下一个链表合并;

此时时间复杂度为O(nk),遗憾,提交会出现TLE。

所以,必须另觅他法,我们知道排序领域中很多方法都可以提高性能比如合并排序性能为O(nlogn);可以应用到此题,可以顺利AC,时间复杂度为O(nlogk)

AC代码

class Solution {
public:
/*方法一:采用合并排序的思路 复杂度为O(nlogk)*/
ListNode* mergeKLists(vector<ListNode*>& lists) {
int len = lists.size();
ListNode *head = NULL;
if (len == 0)
return head;
else if (len == 1)
{
head = lists[0];
return head;
}
else{
int mid = (len - 1) / 2;
vector<ListNode *> l1(lists.begin(), lists.begin() + mid + 1);
vector<ListNode *> l2(lists.begin() + mid + 1, lists.end());
ListNode *head1 = mergeKLists(l1);
ListNode *head2 = mergeKLists(l2); return mergeTwoLists(head1, head2);
}//else
} /*方法二:暴力法 复杂度为O(nK) 出现超时*/
ListNode* mergeKLists2(vector<ListNode*>& lists) {
int len = lists.size();
ListNode *head = NULL;
if (len == 0)
return head;
else if (len == 1)
{
head = lists[0];
return head;
}
else{
head = mergeTwoLists(lists[0], lists[1]);
for (int i = 2; i < len; i++)
head = mergeTwoLists(head, lists[i]);
}
return head;
} /*合并两个链表函数*/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1; //合并后链表初始化为空
ListNode *rl = NULL; ListNode *p = l1, *q = l2;
if (l1->val <= l2->val)
{
rl = l1;
p = l1->next;
}
else{
rl = l2;
q = l2->next;
}
rl->next = NULL;
ListNode *head = rl; while (p && q)
{
if (p->val <= q->val)
{
rl->next = p;
p = p->next;
}
else{
rl->next = q;
q = q->next;
}//else
rl = rl->next;
}//while while (p)
{
rl->next = p;
p = p->next;
rl = rl->next;
} while (q)
{
rl->next = q;
q = q->next;
rl = rl->next;
} return head;
}
};

GitHub测试程序源码

LeetCode(23)Merge k Sorted Lists的更多相关文章

  1. 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 splicin ...

  2. leetcode第22题--Merge k Sorted Lists

    problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...

  3. Leetcode(23)-合并K个排序链表

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: 1-&g ...

  4. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

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

  5. 23. Merge k Sorted Lists - LeetCode

    Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...

  6. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  7. python 中的堆 (heapq 模块)应用:Merge K Sorted Lists

    堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...

  8. Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

  9. Merge k Sorted Lists——分治与堆排序(需要好好看)

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

随机推荐

  1. openstack安装newton版本Glance部署(二)

    一.部署Glance 1.Glance 安装 [root@linux-node1 ~]#yum install openstack-glance python-glance python-glance ...

  2. vconsole移动端调试技巧(禁止webviuew,inspect等)

    如果由于某种原因(天朝FQ),不能支持google 的 inspect  调试 或者再想在某个APP里面调试你的页面,但是没有打开APP的webview ,也不能授权调试 在或者,Fider 可以拦截 ...

  3. 洛谷P1781 宇宙总统

    https://www.luogu.org/problem/show?pid=1781 高精比较大小: #include<iostream> #include<cstdio> ...

  4. fleet - 基于Machine Metadata的任务调度

    基于Machine Metadata的任务调度 复杂和特定的要求的应用程序可以针对一个子集的集群调度通过machine metadata.强大的部署拓扑结构,可以实现的基础上的机器的地区,机架位置,磁 ...

  5. JAVA 员工管理系统(用抽象类实现),简易版。

    package Demo513; /* 定义一个Employee类,该类包含: private 成员变量name,number,birthday,其中birthday为MyDate类的对象: abst ...

  6. HTML5 JSDOM

    1,HTML5 新语义化标签 - nav -- 表示导航 - header -- 表示页眉 -- 头部 - section -- 表示区块 -- 类似于div - main -- 文档主要内容 - a ...

  7. Kendo UI 单页面应用(二) Router 类

    Kendo UI 单页面应用(二) Router 类 Route 类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route 通过 Url 的片段功能(#url)和流量器的浏览历史功能融合在一 ...

  8. (笔记)快速入门PADS logic 到 layout

    以前从未接触过画板,先是硬着头皮边学边操作<Layout2007中文教程之PADS_Logic>,刚好在中秋节前把这个教程从头到尾通学了一遍,随后感觉这个教程有了方方面面但没有工程的系统性 ...

  9. 判断JS数据类型的几种方法

    原文转自http://www.cnblogs.com/onepixel/p/5126046.html! 说到数据类型,我们先说一下JavaScript 中常见的几种数据类型: 基本类型:string, ...

  10. 创建XML的用法

    注意:在实际开发中,注意createElement().createAttribute().createTextNode().appendchild()等方法的具体使用. // root根节点的属性数 ...