# [2. 两数相加](https://leetcode-cn.com/problems/add-two-numbers) [English Version](/solution/0000-0099/0002.Add%20Two%20Numbers/README_EN.md) ## 题目描述 <!-- 这里写题目描述 --> <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://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0002.Add%20Two%20Numbers/images/addtwonumber1.jpg" style="width: 483px; height: 342px;" /> <pre> <strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4] <strong>输出:</strong>[7,0,8] <strong>解释:</strong>342 + 465 = 807. </pre> <p><strong>示例 2:</strong></p> <pre> <strong>输入:</strong>l1 = [0], l2 = [0] <strong>输出:</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>输出:</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> ## 解法 <!-- 这里可写通用的实现逻辑 --> <!-- tabs:start --> ### **Python3** <!-- 这里可写当前语言的特殊实现逻辑 --> ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: carry = 0 dummy = ListNode(-1) cur = dummy while l1 or l2 or carry: s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry carry, val = divmod(s, 10) cur.next = ListNode(val) cur = cur.next l1 = None if not l1 else l1.next l2 = None if not l2 else l2.next return dummy.next ``` ### **Java** <!-- 这里可写当前语言的特殊实现逻辑 --> ```java /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry = 0; ListNode dummy = new ListNode(-1); ListNode cur = dummy; while (l1 != null || l2 != null || carry != 0) { int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry; carry = s / 10; cur.next = new ListNode(s % 10); cur = cur.next; l1 = l1 == null ? null : l1.next; l2 = l2 == null ? null : l2.next; } return dummy.next; } } ``` ### **C++** ```cpp /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int carry = 0; ListNode* dummy = new ListNode(-1); ListNode* cur = dummy; while (l1 != NULL || l2 != NULL || carry != 0) { int s = (l1 == NULL ? 0 : l1-> val) + (l2 == NULL ? 0 : l2->val) + carry; carry = s / 10; cur->next = new ListNode(s % 10); cur = cur->next; l1 = l1 == NULL ? NULL : l1->next; l2 = l2 == NULL ? NULL : l2->next; } return dummy->next; } }; ``` ### **JavaScript** ```js /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function (l1, l2) { let carry = 0; const dummy = new ListNode(-1); let cur = dummy; while (l1 || l2 || carry) { const s = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry; carry = Math.floor(s / 10); cur.next = new ListNode(s % 10); cur = cur.next; l1 = l1 ? l1.next : l1; l2 = l2 ? l2.next : l2; } return dummy.next; }; ``` ### **C#** ```cs /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */ public class Solution { public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(-1); ListNode cur = dummy; var carry = 0; while (l1 != null || l2 != null || carry != 0) { int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry; carry = t / 10; cur.next = new ListNode(t % 10); cur = cur.next; l1 = l1 == null ? null : l1.next; l2 = l2 == null ? null : l2.next; } return dummy.next; } } ``` ### **...** ``` ``` <!-- tabs:end -->