# 合并两个有序链表

<p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 </p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/images/merge_ex1.jpg" style="width: 662px; height: 302px;" /><pre><strong>输入：</strong>l1 = [1,2,4], l2 = [1,3,4]<strong><br />输出：</strong>[1,1,2,3,4,4]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>l1 = [], l2 = []<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>l1 = [], l2 = [0]<strong><br />输出：</strong>[0]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>两个链表的节点数目范围是 <code>[0, 50]</code></li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>l1</code> 和 <code>l2</code> 均按 <strong>非递减顺序</strong> 排列</li></ul>

## template

```cpp
struct ListNode
{
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
	ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
	{
		ListNode h(0, nullptr);
		ListNode *p = &h;
		while (l1 && l2)
		{
			ListNode **t;
			if (l1->val < l2->val)
				t = &l1;
			else
				t = &l2;
			p->next = *t;
			p = *t;
			*t = (*t)->next;
		}
		if (l1)
			p->next = l1;
		else
			p->next = l2;
		return h.next;
	}
};
```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```