有关于priority_queue的使用
其实就是加入队列排个序输出
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
class pri {
public:
bool operator()(ListNode* a, ListNode* b)
{
if (a == NULL)
return false;
else if (b == NULL)
return true;
else
return a->val > b->val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty())
return NULL;
priority_queue<ListNode*, vector<ListNode*>, pri>help;
ListNode* head = new ListNode(0);
ListNode* temp_ret = head;
for (auto i : lists)
help.push(i);
while ((!help.empty()))
{
auto temp = help.top();
help.pop();
if (temp != NULL) {
temp_ret->next = temp;
temp_ret = temp_ret->next;
help.push(temp->next);
}
}
return head->next;
}
};