// algorithm.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int x) :val(x){}
};
ListNode* makeList(vector<int>& vec)
{
ListNode* head = new ListNode(vec[0]);
ListNode*pt = head;
for (int i = 1; i < vec.size(); ++i){
pt->next = new ListNode(vec[i]);
pt = pt->next;
}
pt->next = nullptr;
return head;
}
void printList(ListNode* head){
ListNode* pt = head;
while (pt){
cout << pt->val << "->";
pt = pt->next;
}
}
class Cmp{
public:
bool operator()(const ListNode* p1, const ListNode* p2){
if (p1 && p2)
{
return p1->val > p2->val;//小顶堆
}
}
};
auto cmp = [](const ListNode* &a, const ListNode* &b) { return a->val < b->val; };
ListNode* mergeLists(vector<ListNode*>& Lists){
ListNode* resNode = new ListNode(0);
ListNode* pt = resNode;
priority_queue<ListNode*, vector<ListNode* >, Cmp> que;
for (auto x : Lists){
if (x == nullptr){
continue;
}
que.push(x);
}
while (!que.empty()){
ListNode* temp = que.top();
pt->next = temp;
pt = pt->next;
que.pop();
if (temp->next!=nullptr){
que.push(temp->next);
}
}
return resNode->next;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> a1 = { 1, 3 };
vector<int> a2 = { 4, 5 };
vector<int> a3 = { 2, 9 };
ListNode* p1 = makeList(a1);
ListNode* p2 = makeList(a2);
ListNode* p3 = makeList(a3);
vector<ListNode*>pp = { p1, p2, p3 };
printList(mergeLists(pp));
system("pause");
}