# [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><strong>示例:</strong></p> <pre><strong>输入:</strong>(2 -> 4 -> 3) + (5 -> 6 -> 4) <strong>输出:</strong>7 -> 0 -> 8 <strong>原因:</strong>342 + 465 = 807 </pre> ## 解法 <!-- 这里可写通用的实现逻辑 --> <!-- tabs:start --> ### **Python3** <!-- 这里可写当前语言的特殊实现逻辑 --> ```python # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: carry = 0 dummy = ListNode(-1) cur = dummy while l1 or l2 or carry: t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry carry = t // 10 cur.next = ListNode(t % 10) 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() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ 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 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; } } ``` ### **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 -->