/**
* 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. [转]控制反转(IOC)和依赖注入(DI)

    http://blog.csdn.net/Elite_1989/article/details/16851565 控制反转和依赖注入可以理解成同一个东西,都是为解耦而生的~ 控制反转(IoC=Inve ...

  2. 3150 Pibonacci数 - Wikioi

    题目描述 Description 你可能听说过的Fibonacci数和圆周率Pi. 如果你让这两个概念合并,一个新的深奥的概念应运而生:Pibonacci数. 这些数可以被定义为对于x>=0: ...

  3. spring +hibernate 启动优化【转】

    最近在负责一个大项目,项目组成员包括项目经理大概10个人左右.项目技术用struts+spring+hibernate实现.项目的规模相对来说是比较大的,总共有10大模块,每个大模块又分为有十几个.甚 ...

  4. centos7 更新yum安装源

    系统自带的yum安装源有些软件找不到  这里我们使用阿里云的源 1.加源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re ...

  5. SGU 186

    总是拆最短的链子  连接长的链子   贪心.... #include <cstdio> #include <cstring> #include <cmath> #i ...

  6. firefly 框架 结构图

    原地址:http://www.9miao.com/question-15-54838.html 系统结构:

  7. c#调用命令行遇到带空格的路径

    想用 c#调用如下的DOS命令: C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe /f C:\Program File ...

  8. 李洪强iOS开发之静态库

    iOS开发拓展篇—静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开源库 公开源代码,能看到具体实现 ...

  9. Fibonacci sequence 求余数

    #include <iostream> using namespace std; int f(int n); int main() { int n; cin>>n; doubl ...

  10. HeadFirst设计模式之策略模式

    什么是策略模式:它定义了一系列算法,可以根据不同的实现调用不同的算法 大多数的设计模式都是为了解决系统中变化部分的问题 一.OO基础 抽象.封装.多态.继承 二.OO原则 1.封装变化,如把FlyBe ...