# 合并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>

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

```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
	ListNode *mergeKLists(vector<ListNode *> &lists)
	{
		auto cmp = [](struct ListNode *n1, struct ListNode *n2)
		{
			return n1->val > n2->val;
		} priority_queue<struct ListNode *, vector<struct ListNode *>, decltype(cmp)>
			queue(cmp);
		for (int i = 0; i < lists.size(); i++)
		{
			if (lists[i] != nullptr)
			{
				queue.push(lists[i]);
			}
		}
		struct ListNode dummy, *p = &dummy;
		;
		while (!queue.empty())
		{
			_________________;
		}
		return dummy.next;
	}
};
```

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
	ListNode *mergeKLists(vector<ListNode *> &lists)
	{
		auto cmp = [](struct ListNode *n1, struct ListNode *n2)
		{
			return n1->val > n2->val;
		} priority_queue<struct ListNode *, vector<struct ListNode *>, decltype(cmp)>
			queue(cmp);
		for (int i = 0; i < lists.size(); i++)
		{
			if (lists[i] != nullptr)
			{
				queue.push(lists[i]);
			}
		}
		struct ListNode dummy, *p = &dummy;
		;
		while (!queue.empty())
		{
			ListNode *node = queue.top();
			queue.pop();
			p->next = node;
			p = node;
			if (node->next != nullptr)
			{
				queue.push(node->next);
			}
		}
		return dummy.next;
	}
};
```

## 答案

```cpp
ListNode *node = queue.top();
queue.pop();
p->next = node;
p = node;
if (node->next != nullptr)
{
	queue.push(node->next);
}
```

## 选项

### A

```cpp
ListNode *node = queue.top();
queue.pop();
p->next = node;
p = node;
if (node->next != nullptr)
{
	queue.pop(node->next);
}
```

### B

```cpp
ListNode *node = queue.top();
queue.pop();
p = node;
p->next = node;
if (node->next != nullptr)
{
	queue.push(node->next);
}
```

### C

```cpp
ListNode *node = queue.top();
queue.pop();
p = node;
p->next = node;
if (node->next != nullptr)
{
	queue.pop(node->next);
}
```