-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.合并k个升序链表.cpp
48 lines (44 loc) · 1.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
/*
* @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) {}
* };
*/
class Solution {
public:
// 结构体定义需要学习。
struct comp {
bool operator()(ListNode* a, ListNode* b) {
return a->val > b->val;
}
};
// 优先级队列需要学习使用。 然后尝试记录其他方法,比如参考答案中提到的分治。
priority_queue<ListNode*, vector<ListNode*>, comp> q;
ListNode* mergeKLists(vector<ListNode*>& lists) {
// 语法记录,这种遍历情况记录。
for (auto node: lists) {
if (node) q.push(node);
}
ListNode* head = new ListNode();
ListNode* tail = head;
while (!q.empty()) {
ListNode* node = q.top();
q.pop();
tail->next = node;
tail = tail->next;
if (node->next) q.push(node->next);
}
return head->next;
}
};
// @lc code=end