{"id":5235,"date":"2019-06-22T12:46:36","date_gmt":"2019-06-22T19:46:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5235"},"modified":"2019-06-22T13:00:07","modified_gmt":"2019-06-22T20:00:07","slug":"leetcode-23-merge-k-sorted-lists-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-23-merge-k-sorted-lists-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 23. Merge k Sorted Lists"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 23. Merge k Sorted Lists - \u5237\u9898\u627e\u5de5\u4f5c EP252\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/XqA8bBoEdIY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Merge&nbsp;<em>k<\/em>&nbsp;sorted linked lists and return it as one sorted list. Analyze and describe its complexity.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong>\n[\n&nbsp; 1-&gt;4-&gt;5,\n&nbsp; 1-&gt;3-&gt;4,\n&nbsp; 2-&gt;6\n]\n<strong>Output:<\/strong> 1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Min heap<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/23-ep252.png\" alt=\"\" class=\"wp-image-5239\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/23-ep252.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/23-ep252-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/23-ep252-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(nklogk)<br>Space complexity: O(k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* mergeKLists(vector<ListNode*>& lists) {\n    ListNode dummy(0);\n    ListNode *tail = &dummy;\n    \n    auto comp = [](ListNode* a, ListNode* b) { return a->val > b->val; };\n    priority_queue<ListNode*, vector<ListNode*>, decltype(comp)> q(comp);\n    \n    for (ListNode* list : lists) \n      if (list) q.push(list);\n    \n    while (!q.empty()) {\n      tail->next = q.top(); q.pop();      \n      tail = tail->next;\n      if (tail->next) q.push(tail->next);\n    }\n    return dummy.next;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Merge Sort<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nklogk)<br>Space complexity: O(logk) <\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode* mergeKLists(vector<ListNode*>& lists) {\n    return merge(lists, 0, lists.size() - 1);\n  }\nprivate:\n  ListNode* merge(vector<ListNode*>& lists, int l, int r) {\n    if (l > r) return nullptr;\n    if (l == r) return lists[l];\n    if (l + 1 == r) return mergeTwoLists(lists[l], lists[r]);\n    int m = l + (r - l) \/ 2;\n    auto l1 = merge(lists, l, m);\n    auto l2 = merge(lists, m + 1, r);\n    return mergeTwoLists(l1, l2);\n  }\n  ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {\n    ListNode dummy(0);\n    ListNode* tail = &dummy;\n    while (l1 && l2) {\n      if (l1->val > l2->val) swap(l1, l2);                \n      tail->next = l1;\n      l1 = l1->next;\n      tail = tail->next;\n    }        \n    if (l1) tail->next = l1;\n    if (l2) tail->next = l2;        \n    return dummy.next;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Merge&nbsp;k&nbsp;sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [ &nbsp; 1-&gt;4-&gt;5, &nbsp; 1-&gt;3-&gt;4, &nbsp; 2-&gt;6 ]&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[313,217,83],"class_list":["post-5235","post","type-post","status-publish","format-standard","hentry","category-list","tag-divide-and-conquer","tag-hard","tag-list","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5235"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5235\/revisions"}],"predecessor-version":[{"id":5240,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5235\/revisions\/5240"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}