Skip to content

Commit

Permalink
June 21 Daily
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeige4 committed Jun 21, 2022
1 parent 104a2f2 commit c1db095
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions DataStructures/PriorityQueue/LC23(Hard).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <bits/stdc++.h>

using namespace std;

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) {}
};

// 23. 合并K个升序链表

// 给你一个链表数组,每个链表都已经按升序排列。

// 请你将所有链表合并到一个升序链表中,返回合并后的链表。

class cmp{
public:
bool operator()(ListNode* a,ListNode* b){
return a->val>b->val;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*,vector<ListNode*>,cmp> pq;
for(ListNode*& node : lists){
if(node){
pq.push(node);
}
}
ListNode dummyHead = ListNode();
ListNode* pre = &dummyHead;

while(!pq.empty()){
pre->next = pq.top();
pre = pre->next;
ListNode* next = pq.top()->next;
pq.pop();
if(next){
pq.push(next);
}
}
return dummyHead.next;
}
};

0 comments on commit c1db095

Please sign in to comment.