# 两数相加

<p>给你两个 <strong>非空</strong> 的链表，表示两个非负的整数。它们每位数字都是按照 <strong>逆序</strong> 的方式存储的，并且每个节点只能存储 <strong>一位</strong> 数字。</p><p>请你将两个数相加，并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外，这两个数都不会以 0 开头。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/3d2b007db4e6d4c6b2b5ec03d67f97e0.jpeg" style="width: 483px; height: 342px;" /><pre><strong>输入：</strong>l1 = [2,4,3], l2 = [5,6,4]<strong><br />输出：</strong>[7,0,8]<strong><br />解释：</strong>342 + 465 = 807.</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>l1 = [0], l2 = [0]<strong><br />输出：</strong>[0]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<strong><br />输出：</strong>[8,9,9,9,0,0,0,1]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>每个链表中的节点数在范围 <code>[1, 100]</code> 内</li>	<li><code>0 <= Node.val <= 9</code></li>	<li>题目数据保证列表表示的数字不含前导零</li></ul>

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

```cpp
struct ListNode
{
	int val;
	struct ListNode *next;
};
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{
	struct ListNode *pp = NULL, *p = l1;
	struct ListNode *qp = NULL, *q = l2;
	int carry = 0;
	while (p != NULL && q != NULL)
	{
		p->val += q->val + carry;
		carry = 0;
		if (p->val >= 10)
		{
			carry = 1;
			p->val -= 10;
		}
		pp = p;
		p = p->next;
		qp = q;
		q = q->next;
	}
	if (q)
	{
		pp->next = p = q;
		qp->next = NULL;
	}
	____________________;
	if (carry)
	{
		struct ListNode *n = (struct ListNode *)malloc(sizeof(struct ListNode));
		n->val = 1;
		n->next = NULL;
		pp->next = n;
	}
	return l1;
}
```

## template

```cpp
struct ListNode
{
	int val;
	struct ListNode *next;
};
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{
	struct ListNode *pp = NULL, *p = l1;
	struct ListNode *qp = NULL, *q = l2;
	int carry = 0;
	while (p != NULL && q != NULL)
	{
		p->val += q->val + carry;
		carry = 0;
		if (p->val >= 10)
		{
			carry = 1;
			p->val -= 10;
		}
		pp = p;
		p = p->next;
		qp = q;
		q = q->next;
	}
	if (q)
	{
		pp->next = p = q;
		qp->next = NULL;
	}
	while (carry && p)
	{
		p->val += carry;
		carry = 0;
		if (p->val >= 10)
		{
			carry = 1;
			p->val -= 10;
		}
		pp = p;
		p = p->next;
	}
	if (carry)
	{
		struct ListNode *n = (struct ListNode *)malloc(sizeof(struct ListNode));
		n->val = 1;
		n->next = NULL;
		pp->next = n;
	}
	return l1;
}
```

## 答案

```cpp
while (carry && p)
{
	p->val += carry;
	carry = 0;
	if (p->val >= 10)
	{
		carry = 1;
		p->val -= 10;
	}
	pp = p;
	p = p->next;
}
```

## 选项

### A

```cpp
while (carry && p)
{
	p->val += carry;
	carry = 0;
	if (p->val >= 10)
	{
		carry = 1;
		p->val -= 10;
	}
	p = p->next;
	pp = p;
}
```

### B

```cpp
while (carry && p)
{
	p->val += carry;
	carry = 0;
	if (p->val >= 10)
	{
		carry += 1;
		p->val -= 10;
	}
	p = p->next;
	pp = p;
}
```

### C

```cpp
while (carry && p)
{
	p->val += carry;
	carry = 0;
	if (p->val >= 10)
	{
		carry += 1;
		p->val -= 10;
	}
	p = p->next;
	pp = p;
}
```