# 23.merge-k-sorted-lists

## Statement

• Difficulty: Hard
• Tag: `链表` `分治` `堆（优先队列）` `归并排序`

``````输入：lists = [[1,4,5],[1,3,4],[2,6]]

[
1->4->5,
1->3->4,
2->6
]

1->1->2->3->4->4->5->6
``````

``````输入：lists = []

``````

``````输入：lists = [[]]

``````

• `k == lists.length`
• `0 <= k <= 10^4`
• `0 <= lists[i].length <= 500`
• `-10^4 <= lists[i][j] <= 10^4`
• `lists[i]`升序 排列
• `lists[i].length` 的总和不超过 `10^4`

• Link: Merge k Sorted Lists
• Difficulty: Hard
• Tag: `Linked List` `Divide and Conquer` `Heap (Priority Queue)` `Merge Sort`

You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order.

Example 1:

``````Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6
``````

Example 2:

``````Input: lists = []
Output: []
``````

Example 3:

``````Input: lists = [[]]
Output: []
``````

Constraints:

• `k == lists.length`
• `0 <= k <= 104`
• `0 <= lists[i].length <= 500`
• `-104 <= lists[i][j] <= 104`
• `lists[i]` is sorted in ascending order.
• The sum of `lists[i].length` will not exceed `104`.

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

#ifdef LOCAL

struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};

#endif
/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/

template <typename T>
struct cmp : public binary_function<T, T, bool> {
constexpr bool operator()(const T &l, const T &r) const {
if (!l) {
return false;
}

if (!r) {
return true;
}

return l->val > r->val;
}
};

class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode *, vector<ListNode *>, cmp<ListNode *>> pq;

auto rt = new ListNode();
auto res = rt;

for (const auto p : lists) {
pq.push(p);
}

while (!pq.empty()) {
auto p = pq.top();
pq.pop();

if (!p) {
continue;
}

rt->next = new ListNode();
rt = rt->next;
rt->val = p->val;

pq.push(p->next);
}

return res->next;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````