{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "d155a0ab95f9496fa9647cf8ccf6b9a9",
  "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": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "from typing import List\nclass ListNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.next = None\nclass LinkList:\n\tdef __init__(self):\n\t\tself.head=None\n\tdef initList(self, data):\n\t\tself.head = ListNode(data[0])\n\t\tr=self.head\n\t\tp = self.head\n\t\tfor i in data[1:]:\n\t\t\tnode = ListNode(i)\n\t\t\tp.next = node\n\t\t\tp = p.next\n\t\treturn r\n\tdef\tconvert_list(self,head):\n\t\tret = []\n\t\tif head == None:\n\t\t\treturn\n\t\tnode = head\n\t\twhile node != None:\n\t\t\tret.append(node.val)\n\t\t\tnode = node.next\n\t\treturn ret\nclass Solution(object):\n\tdef mergeKLists(self, lists):\n\t\tif lists is None:\n\t\t\treturn None\n\t\telif len(lists) == 0:\n\t\t\treturn None\n\t\treturn self.mergeK(lists, 0, len(lists) - 1)\n\tdef mergeK(self, lists, low, high):\n\t\tif low == high:\n\t\t\treturn lists[int(low)]\n\t\telif low + 1 == high:\n\t\t\treturn self.mergeTwolists(lists[int(low)], lists[int(high)])\n\t\tmid = (low + high) / 2\n\t\treturn self.mergeTwolists(self.mergeK(lists, low, mid), self.mergeK(lists, mid + 1, high))\n\tdef mergeTwolists(self, l1, l2):\n\t\tl = LinkList()\n\t\tif type(l1) == list:\t\n\t\t\tl1 = l.initList(l1)\n\t\tif type(l2) == list:\t\n\t\t\tl2 = l.initList(l2)\n\t\tif l1 is None:\n\t\t\treturn l2\n\t\tif l2 is None:\n\t\t\treturn l1\n\t\thead = curr = ListNode(-1)\n\t\twhile l1 is not None and l2 is not None:\n\t\t\tif l1.val <= l2.val:\n\t\t\t\tcurr.next = l1\n\t\t\t\tl1 = l1.next\n\t\t\telse:\n\t\t\t\tcurr.next = l2\n\t\t\t\tl2 = l2.next\n\t\t\tcurr = curr.next\n\t\tif l1 is not None:\n\t\t\tcurr.next = l1\n\t\tif l2 is not None:\n\t\t\tcurr.next = l2\n\t\treturn head.next\n# %%\nl = LinkList()\nlist1 = [[1,4,5],[1,3,4],[2,6]]\ns = Solution()\nprint(l.convert_list(s.mergeKLists(list1)))",
    "language": "python"
  },
  "node_id": "dailycode-43a60df3112046ceabce5a6c72c802af",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470116"
}