{"id":4117,"date":"2018-10-02T01:02:55","date_gmt":"2018-10-02T08:02:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4117"},"modified":"2018-10-02T01:13:43","modified_gmt":"2018-10-02T08:13:43","slug":"leetcode-23-merge-k-sorted-lists","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-23-merge-k-sorted-lists\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 23. Merge k Sorted Lists"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Merge\u00a0<em>k<\/em>\u00a0sorted linked lists and return it as one sorted list. Analyze and describe its complexity.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong>\r\n[\r\n\u00a0 1-&gt;4-&gt;5,\r\n\u00a0 1-&gt;3-&gt;4,\r\n\u00a0 2-&gt;6\r\n]\r\n<strong>Output:<\/strong> 1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6<\/pre>\n<h1><strong>Solution 1: Brute Force<\/strong><\/h1>\n<p>Time complexity: O(nk)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 420 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* mergeKLists(vector&lt;ListNode*&gt;&amp; lists) {\r\n    ListNode dummy(0);\r\n    ListNode *cur = &amp;dummy;\r\n    while (true) {\r\n      ListNode** min_head = nullptr;\r\n      for (auto&amp; head : lists) {          \r\n        if (!head) continue;        \r\n        if (!min_head || head-&gt;val &lt; (*min_head)-&gt;val)\r\n          min_head = &amp;head;\r\n      }\r\n      if (!min_head) break;\r\n      cur-&gt;next = new ListNode((*min_head)-&gt;val);\r\n      cur = cur-&gt;next;\r\n      *min_head = (*min_head)-&gt;next;      \r\n    }\r\n    return dummy.next;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Heap \/ Priority Queue<\/strong><\/h1>\n<p>Time complexity: O(nlogk)<\/p>\n<p>Space complexity: O(k)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 16 ms (&lt;99.88%)\r\nclass Solution {\r\npublic:\r\n  ListNode* mergeKLists(vector&lt;ListNode*&gt;&amp; lists) {\r\n    ListNode dummy(0);\r\n    ListNode *cur = &amp;dummy;\r\n    \r\n    auto comp = [](ListNode* a, ListNode* b) { return a-&gt;val &gt; b-&gt;val; };\r\n    priority_queue&lt;ListNode*, vector&lt;ListNode*&gt;, decltype(comp)&gt; q(comp);\r\n    \r\n    for (ListNode* list : lists) \r\n      if (list) q.push(list);\r\n    \r\n    while (!q.empty()) {\r\n      cur-&gt;next = q.top(); q.pop();      \r\n      cur = cur-&gt;next;\r\n      if (cur-&gt;next) q.push(cur-&gt;next);\r\n    }\r\n    return dummy.next;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Merge\u00a0k\u00a0sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [ \u00a0 1-&gt;4-&gt;5, \u00a0 1-&gt;3-&gt;4, \u00a0 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":[73,83,129],"class_list":["post-4117","post","type-post","status-publish","format-standard","hentry","category-list","tag-heap","tag-list","tag-merge","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4117","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=4117"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4117\/revisions"}],"predecessor-version":[{"id":4121,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4117\/revisions\/4121"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}