版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tangyuanzong/article/details/85016749
题目:https://leetcode.com/problems/merge-k-sorted-lists/
合并n个排序链表,直接合并即可。
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode * head = new ListNode(0);
ListNode * t = head;
while(1){
int temp = -1;
for(int i = 0; i<lists.size();i++){
if(!lists[i]) continue;
if(temp < 0) temp = i;
else if(temp>=0 && lists[temp]->val > lists[i]->val) temp = i;
}
if(temp<0) break;
t->next = lists[temp];
t = t->next;
lists[temp] = lists[temp]->next;
}
ListNode *ret = head->next;
delete head;
return ret;
}
};