题目:https://leetcode-cn.com/problems/merge-k-sorted-lists/submissions/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
struct cmp{//优队使用配套cmp
bool operator () (const ListNode *a,const ListNode *b) {
return a->val > b->val;
}
};
ListNode* mergeKLists(vector& lists) {
/*
*优队链表
*将有序链表都丢进队列,每次取最小的出来即可
*
*/
priority_queue q;//链表类优队
int n = lists.size();
for(int i = 0;i next = new ListNode(cur->val);
p = p->next;
if(cur->next != NULL) q.push(cur->next);
}
return ans->next;
}
};