/**
* 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:
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode* dummyhead = new ListNode(0);
ListNode* prev = dummyhead;
int len = 0;
for(auto list : lists){
ListNode* head = list;
while(head != nullptr){
head = head->next;
len++;
}
}
while(len--){
ListNode* min = nullptr;
int k = -1;
for(int i = 0; i < lists.size(); i++){
if(min == nullptr && lists[i] != nullptr) {
min = lists[i];
k = i;
}
if(lists[i] != nullptr && lists[i]->val < min->val) {
min = lists[i];
k = i;
}
}
prev->next = min;
prev = prev->next;
lists[k] = lists[k]->next;
}
return dummyhead->next;
}
};
05-04
1492
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-11
446
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-25
714
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-11
326
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)