diff --git a/cpp/2303/230312.cpp b/cpp/2303/230312.cpp new file mode 100644 index 0000000..714e691 --- /dev/null +++ b/cpp/2303/230312.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {} +}; + +ListNode* construct(int x) { + return new ListNode(x); +} + +template +ListNode* construct(int x, Ts... xs) { + return new ListNode(x, construct(xs...)); +} + +struct cmp { + bool operator()(const ListNode* x, const ListNode* y) { + return x && (!y || x->val > y->val); + } +}; + +/** + * 23. Merge k Sorted Lists + * + * You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. + * Merge all the linked-lists into one sorted linked-list and return it. + */ + +class Solution { +public: + static ListNode* mergeKLists(const std::vector& lists) { + if (lists.empty()) + return nullptr; + std::priority_queue, cmp> q(lists.begin(), lists.end()); + ListNode* ptr = new ListNode(0), * head = ptr; + for (; !q.empty() && !q.top(); q.pop()); + while (!q.empty()) { + ListNode* p = q.top(); + q.pop(); + ptr = ptr->next = new ListNode(p->val); + if (p->next) + q.push(p->next); + } + return head->next; + } +}; + +int main() { + ListNode* h = Solution::mergeKLists({ + construct(1, 4, 5), + nullptr, + construct(1, 3, 4), + construct(2, 6), + nullptr, + nullptr + }); + for (ListNode* ptr = h; ptr; ptr = ptr->next) + std::cout << ptr->val << "\n"; + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index e7c46ae..56583d9 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230311.cpp) +ADD_EXECUTABLE(2303 230312.cpp)