-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.合并k个升序链表.cpp
93 lines (80 loc) · 2.22 KB
/
23.合并k个升序链表.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @lc app=leetcode.cn id=23 lang=cpp
*
* [23] 合并K个升序链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
struct cmp{
bool operator()(ListNode * a, ListNode * b) {return a->val > b->val;}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
// 使用 优先队列
priority_queue<ListNode *, vector<ListNode*>, cmp> q;
// 把队头 全部进 栈。
for(auto i: lists) if (i) q.push(i);
ListNode * head = new ListNode(0);
ListNode * temp = head;
while (q.size()) {
ListNode * t = q.top();
q.pop();
temp->next = t;
temp = t;
if (t->next) q.push(t->next);
}
return head->next;
}
};
/* 方法二
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty()) {
return nullptr;
}
return merge(lists, 0, lists.size() - 1);
}
ListNode* merge(vector<ListNode*>& lists, int left, int right) {
if (left == right) {
return lists[left];
} else if (left + 1 == right) {
return mergeTwoLists(lists[left], lists[right]);
}
int mid = left + (right - left) / 2;
return mergeTwoLists(merge(lists, left, mid), merge(lists, mid + 1, right));
}
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
auto head = new ListNode(-1);
auto tail = head;
while (l1 || l2) {
auto val1 = l1 ? l1->val : INT_MAX;
auto val2 = l2 ? l2->val : INT_MAX;
if (val1 < val2) {
tail->next = l1;
tail = tail->next;
l1 = l1->next;
} else {
tail->next = l2;
tail = tail->next;
l2 = l2->next;
}
}
auto dummy = head;
head = head->next;
delete dummy;
return head;
}
};
*/
// @lc code=end