{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "1406e7cd7c1f44d3a9e63e9c3ff1cfe6",
  "keywords": "链表,分治,堆（优先队列）,归并排序",
  "title": "合并K个升序链表",
  "desc": [
    {
      "content": "\n<p>给你一个链表数组，每个链表都已经按升序排列。</p><p>请你将所有链表合并到一个升序链表中，返回合并后的链表。</p><p>&nbsp;</p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>lists = [[1,4,5],[1,3,4],[2,6]]<strong><br />输出：</strong>[1,1,2,3,4,4,5,6]<strong><br />解释：</strong>链表数组如下：[  1-&gt;4-&gt;5,  1-&gt;3-&gt;4,  2-&gt;6]将它们合并到一个有序链表中得到。1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>lists = []<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>lists = [[]]<strong><br />输出：</strong>[]</pre><p>&nbsp;</p><p><strong>提示：</strong></p><ul>\t<li><code>k == lists.length</code></li>\t<li><code>0 &lt;= k &lt;= 10^4</code></li>\t<li><code>0 &lt;= lists[i].length &lt;= 500</code></li>\t<li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li>\t<li><code>lists[i]</code> 按 <strong>升序</strong> 排列</li>\t<li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nstruct ListNode\n{\n\tint val;\n\tListNode *next;\n\tListNode() : val(0), next(nullptr) {}\n\tListNode(int x) : val(x), next(nullptr) {}\n\tListNode(int x, ListNode *next) : val(x), next(next) {}\n};\nclass Solution\n{\npublic:\n\tListNode *mergeKLists(vector<ListNode *> &lists)\n\t{\n\t\tauto cmp = [](struct ListNode *n1, struct ListNode *n2)\n\t\t{\n\t\t\treturn n1->val > n2->val;\n\t\t} priority_queue<struct ListNode *, vector<struct ListNode *>, decltype(cmp)>\n\t\t\tqueue(cmp);\n\t\tfor (int i = 0; i < lists.size(); i++)\n\t\t{\n\t\t\tif (lists[i] != nullptr)\n\t\t\t{\n\t\t\t\tqueue.push(lists[i]);\n\t\t\t}\n\t\t}\n\t\tstruct ListNode dummy, *p = &dummy;\n\t\t;\n\t\twhile (!queue.empty())\n\t\t{\n\t\t\tListNode *node = queue.top();\n\t\t\tqueue.pop();\n\t\t\tp->next = node;\n\t\t\tp = node;\n\t\t\tif (node->next != nullptr)\n\t\t\t{\n\t\t\t\tqueue.push(node->next);\n\t\t\t}\n\t\t}\n\t\treturn dummy.next;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-d1dc9f58aa4b411886252ef1dbc58060",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470116"
}