# 合并K个升序链表

<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>	<li><code>k == lists.length</code></li>	<li><code>0 &lt;= k &lt;= 10^4</code></li>	<li><code>0 &lt;= lists[i].length &lt;= 500</code></li>	<li><code>-10^4 &lt;= lists[i][j] &lt;= 10^4</code></li>	<li><code>lists[i]</code> 按 <strong>升序</strong> 排列</li>	<li><code>lists[i].length</code> 的总和不超过 <code>10^4</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```python
from typing import List


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class LinkList:
    def __init__(self):
        self.head = None

    def initList(self, data):
        self.head = ListNode(data[0])
        r = self.head
        p = self.head
        for i in data[1:]:
            node = ListNode(i)
            p.next = node
            p = p.next
        return r

    def convert_list(self, head):
        ret = []
        if head == None:
            return
        node = head
        while node != None:
            ret.append(node.val)
            node = node.next
        return ret


class Solution(object):
    def mergeKLists(self, lists):
        if lists is None:
            return None
        elif len(lists) == 0:
            return None
        return self.mergeK(lists, 0, len(lists) - 1)

    def mergeK(self, lists, low, high):
        if low == high:
            return lists[int(low)]
        elif low + 1 == high:
            return self.mergeTwolists(lists[int(low)], lists[int(high)])
        mid = (low + high) / 2
        return self.mergeTwolists(self.mergeK(lists, low, mid),
                                  self.mergeK(lists, mid + 1, high))

    def mergeTwolists(self, l1, l2):
        l = LinkList()
        if type(l1) == list:
            l1 = l.initList(l1)
        if type(l2) == list:
            l2 = l.initList(l2)
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        head = curr = ListNode(-1)
        while l1 is not None and l2 is not None:
			____________________;
            curr = curr.next
        if l1 is not None:
            curr.next = l1
        if l2 is not None:
            curr.next = l2
        return head.next


# %%
l = LinkList()
list1 = [[1, 4, 5], [1, 3, 4], [2, 6]]
s = Solution()
print(l.convert_list(s.mergeKLists(list1)))
```

## template

```python
from typing import List


class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None


class LinkList:
    def __init__(self):
        self.head = None

    def initList(self, data):
        self.head = ListNode(data[0])
        r = self.head
        p = self.head
        for i in data[1:]:
            node = ListNode(i)
            p.next = node
            p = p.next
        return r

    def convert_list(self, head):
        ret = []
        if head == None:
            return
        node = head
        while node != None:
            ret.append(node.val)
            node = node.next
        return ret


class Solution(object):
    def mergeKLists(self, lists):
        if lists is None:
            return None
        elif len(lists) == 0:
            return None
        return self.mergeK(lists, 0, len(lists) - 1)

    def mergeK(self, lists, low, high):
        if low == high:
            return lists[int(low)]
        elif low + 1 == high:
            return self.mergeTwolists(lists[int(low)], lists[int(high)])
        mid = (low + high) / 2
        return self.mergeTwolists(self.mergeK(lists, low, mid),
                                  self.mergeK(lists, mid + 1, high))

    def mergeTwolists(self, l1, l2):
        l = LinkList()
        if type(l1) == list:
            l1 = l.initList(l1)
        if type(l2) == list:
            l2 = l.initList(l2)
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        head = curr = ListNode(-1)
        while l1 is not None and l2 is not None:
			list1 = [[1, 4, 5], [1, 3, 4], [2, 6]]
			s = Solution()
			print(l.convert_list(s.mergeKLists(list1)))
            curr = curr.next
        if l1 is not None:
            curr.next = l1
        if l2 is not None:
            curr.next = l2
        return head.next


# %%
l = LinkList()
list1 = [[1, 4, 5], [1, 3, 4], [2, 6]]
s = Solution()
print(l.convert_list(s.mergeKLists(list1)))
```

## 答案

```python
if l1.val <= l2.val:
	curr.next = l1
	l1 = l1.next
else:
	curr.next = l2
	l2 = l2.next
```

## 选项

### A

```python
if l1.val >= l2.val:
	curr.next = l1
	l1 = l1.next
else:
	curr.next = l2
	l2 = l2.next
```

### B

```python
if l1.val <= l2.val:
	curr.next = l2
	l2 = l2.next
else:
	curr.next = l1
	l1 = l1.next
```

### C

```python
if l1.val > l2.val:
	curr.next = l1
	l1 = l1.next
else:
	curr.next = l2
	l2 = l2.next
```