题意

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

将K个排好序的链表合并成一个

解法

和之前合并两个有序链表那题不同,那题用的是两个指针来分别记录两个链表中的位置,将小的那个插入新链表然后指针右移,有点像归并排序时用到的方法。这题如果用K个指针来记录的话,每次需要找出最小的那个值,比较麻烦,所以采用的优先队列,首先将所有链表的第一个值入队,然后每次将值最小的一个出队插入新链表里,再将这个元素的下一个元素入队。

class Solution
{
struct Node
{
int val;
ListNode * next = nullptr;
bool operator <(const Node & r) const
{
return val > r.val;
}
};
public:
ListNode* mergeKLists(vector<ListNode*>& lists)
{
int count = lists.size();
priority_queue<Node> que; for(int i = 0;i < lists.size();i ++)
{
Node temp;
if(lists[i])
{
temp.val = lists[i] -> val;
temp.next = lists[i] -> next;
que.push(temp);
}
} ListNode * head = nullptr;
ListNode * cur = nullptr;
while(!que.empty())
{
ListNode * node = new ListNode(0);
Node poped = que.top();
que.pop();
node -> val = poped.val;
node -> next = poped.next;
if(poped.next)
{
Node temp;
temp.val = poped.next -> val;
temp.next = poped.next -> next;
que.push(temp);
} if(head == nullptr)
{
head = node;
cur = node;
}
else
{
cur -> next = node;
cur = cur -> next;
}
}
return head;
}
};

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

  1. LeetCode: Merge k Sorted Lists 解题报告

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

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

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

  3. [Leetcode] Merge k sorted lists 合并k个已排序的链表

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

  4. LeetCode:Merge k Sorted Lists

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

  5. LeetCode——Merge k Sorted Lists

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

  6. leetcode -- Merge k Sorted Lists add code

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

  7. Leetcode:Merge k Sorted Lists分析和实现

    题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...

  8. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

  9. LeetCode —— Merge k Sorted Lists

    /* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...

随机推荐

  1. java网页数据抓取实例

    在很多行业中,要对行业数据进行分类汇总,及时分析行业数据,对于公司未来的发展,有很好的参照和横向对比.所以,在实际工作,我们可能要遇到数据采集这个概念,数据采集的最终目的就是要获得数据,提取有用的数据 ...

  2. NPOI 导出Excel 数据方式

    使用NPOI的库进行Excel导出操作 公共帮助类: using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using S ...

  3. SDN2017 第二次实验作业

    安装floodlight 参考链接:http://www.sdnlab.com/19189.html 从github下载源码,并编译安装 $ sudo apt-get install build-es ...

  4. Linux vsftpd 配置文件详解

    .默认配置: >允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES >匿名用户使用的登陆名为ftp或anonymous,口令为空:匿名 ...

  5. postMessage 消息传递

    点击查看demo 前言 web开发了,除了前台与服务器交换数据,还有可能前台页面间需要进行数据传递,比如窗口间,页面和嵌套的iframe间.这些问题之前都有解决办法,但是现在html5引入的messa ...

  6. HTML页面打印分页标签样式

    <html> <head>     <meta name="vs_targetSchema" content="http://schemas ...

  7. docker-compose.md

    安装 pip python 2.7+的系统同yum先安装pip命令. # yum install -y python2-pip # pip install docker-compose 网络安装 # ...

  8. Python csv.md

    csv csv模块可以用于处理从电子表格和数据库导出的数据到带有字段和记录格式的文本文件,通常称为逗号分隔值(csv)格式,因为逗号通常用于分隔记录中的字段. Reading csv.reader(c ...

  9. BZOJ3236:[AHOI2013]作业(莫队,分块)

    Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 ...

  10. Java POI单元格使用心得

    1: /** * Created by liuguangxin on 2018/5/16. * <p> * MergeRegion:表示excel中cell的信息,startRow与end ...