-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeKorderedList.h
70 lines (64 loc) · 1.45 KB
/
mergeKorderedList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by so_go on 2020/3/30.
//
/*
* 23. 合并K个排序链表
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
*/
#ifndef SRC_MERGEKORDEREDLIST_H
#define SRC_MERGEKORDEREDLIST_H
#include<bits/stdc++.h>
using namespace std;
//* Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
struct compare{
bool operator ()(ListNode *a, ListNode *b){
if(a->val > b->val){
return true;
}
return false;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode *, vector<ListNode*>, compare> hp;
for(ListNode *node : lists){
if(node != NULL){
hp.push(node);
}
}
ListNode *start = NULL;
if(not hp.empty()){
start = hp.top();
hp.pop();
if(start->next != NULL){
hp.push(start->next);
}
}
ListNode *cur = start;
while(not hp.empty()){
ListNode *tp = hp.top();
cur->next = tp;
hp.pop();
if(tp->next != NULL){
hp.push(tp->next);
}
cur = tp;
}
return start;
}
};
#endif //SRC_MERGEKORDEREDLIST_H