/**
* 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;
}
};
标签:力扣,ListNode,val,int,nullptr,lists,next,链表,升序
From: https://blog.csdn.net/why_12134/article/details/141224519