/
DeC2018
/
0023
Обзор
Документация
Войти
/
DeC2018
/
0023
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
62 строки
2 KB
Den
create main.cpp
26 дек 2024, 21:59
26 дек 2024, 21:59
0f38281
Код
Авторство
О чём код?
#include <iostream> #include <vector> #include <queue> 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) {} }; class Solution { public: ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode dummy(0); ListNode* curr = &dummy; auto compare = [](ListNode* a, ListNode* b) { return a->val > b->val; }; priority_queue<ListNode*, vector<ListNode*>, decltype(compare)> minHeap(compare); for (ListNode* list : lists) if (list != nullptr) minHeap.push(list); while (!minHeap.empty()) { ListNode* minNode = minHeap.top(); minHeap.pop(); if (minNode->next) minHeap.push(minNode->next); curr->next = minNode; curr = curr->next; } return dummy.next; } }; void printList(ListNode* head) { cout << "["; while (head) { cout << head->val; if (head->next) cout << ","; head = head->next; } cout << "]"; } int main() { Solution solution; ListNode* list1 = new ListNode(1, new ListNode(4, new ListNode(5))); ListNode* list2 = new ListNode(1, new ListNode(3, new ListNode(4))); ListNode* list3 = new ListNode(2, new ListNode(6)); vector<ListNode*> lists = {list1, list2, list3}; ListNode* mergedList = solution.mergeKLists(lists); printList(mergedList); return 0; }