/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/ struct helper {
ListNode *head;
int len;
helper(ListNode *h, int l) : head ( h ), len ( l ) {}
}; class helpercmp {
public:
bool operator() (const helper &a, const helper &b) {
return a.len > b.len;
}
}; class Solution {
public:
priority_queue<helper, vector<helper>, helpercmp> heap; inline int listSize(ListNode *head) {
int len = ;
for ( ; head != nullptr; head = head->next, len++ );
return len;
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if ( lists.empty() ) return nullptr;
if ( lists.size() == ) return lists[];
ListNode *head = nullptr, *left = nullptr, *right = nullptr;
for ( auto list : lists ) {
heap.push( helper(list, listSize(list) ) );
}
while ( heap.size() != ) {
left = heap.top().head;
heap.pop();
right = heap.top().head;
heap.pop();
head = mergeList( left, right );
heap.push( helper( head, listSize(head) ) );
}
return heap.top().head;
}
ListNode *mergeList(ListNode *a, ListNode *b) {
ListNode dummy();
ListNode *tail = &dummy;
while ( a != nullptr && b != nullptr ) {
if ( a-> val <= b->val ) {
tail->next = a;
a = a->next;
} else {
tail->next = b;
b = b->next;
}
tail = tail->next;
}
tail->next = a == nullptr ? b : a;
return dummy.next;
}
};

Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution的更多相关文章

  1. 蜗牛慢慢爬 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 ...

  2. Java for LeetCode 023 Merge k Sorted Lists

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

  3. 【leetcode】Merge k Sorted Lists

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

  4. LeetCode 023 Merge k Sorted Lists

    题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...

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

  6. [leetcode 23]Merge k Sorted Lists

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

  7. [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆

    转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...

  8. leetcode 【 Merge k Sorted Lists 】python 实现

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

  9. leetcode 23. Merge k Sorted Lists(堆||分治法)

    Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...

随机推荐

  1. The 11th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Pokemon Master

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3776 题意:比较两组数据的总和大小. #include <iostr ...

  2. asp 文件上传(ASPUpload组件上传)

    要实现该功能,就要利用一些特制的文件上传组件.文件上传组件网页非常多,这里介绍国际上非常有名的ASPUpload组件 1 下载和安装ASPUpload   要实现该功能,就要利用一些特制的文件上传组件 ...

  3. uva 10562

    二叉树的先序遍历  这个还是比较简单的 ~~ /************************************************************************* &g ...

  4. CF135A Replacement

    http://codeforces.com/problemset/problem/135/A 题意 : 我能说我卡在这个题的题意上很久吗.....这个题就是在数组里找一个数,然后找另一个数把他替换掉, ...

  5. POJ1151+线段树+扫描线

    /* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

  6. linux samba.tar.gz安装和配置

    安装步骤: 1. tar -xzvf samba-3.5.10.tar.gz2. cd samba-3.5.103. cd source34. ./autogen.sh  如果出现:./autogen ...

  7. linux 模拟延时和丢包

    这是 RHCA 中的一个 BDP 的测试,这也是公司很常用的一种延时和丢包的模拟,现在分享给大家. 我们做的应用软件,还有测试 TCP/UDP  对比,测试 BDP 对 TCP/IP 的影响时,我们都 ...

  8. Let's go! (Ubuntu下搭建Go语言环境)

    自2009年Go语言发布以来,我一直在关注Go语言,如今Go语言已经发展到1.2版本,而且也收到越来越多的人关注这门语言.Go语言设计的目的就是为了解决执行数度快但是编译数度并不理想(如C++)以及编 ...

  9. 在自己的网站上实现QQ授权登录

    最近在实现QQ授权登录,现将我的实现过程以及我的理解整理如下.以下所述如有不对之处,请指正. 官方提供的SDK有:JS,PHP,Java.我的网站使用Scala+Play搭建的,所以只能用JS SDk ...

  10. SRM 585 DIV1 L2

    记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include & ...