# Leetcode 2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode pre(0);
ListNode* p=&pre;
int carry=0;
while(l1||l2||carry){
int sum=(l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
carry=sum/10;
p->next=new ListNode(sum%10);
p=p->next;
l1 = l1 ? l1->next : l1;
l2 = l2 ? l2->next : l2;
}
return pre.next;
}
};

#### 【LeetCode】2. Add Two Numbers 解题报告

2016-10-24 19:46:34

#### LeetCode 2. Add Two Numbers（C++）

2017-03-21 20:11:37

#### 【LeetCode with Python】 Add Two Numbers

2014-06-27 18:11:19

#### leetcode 2. Add Two Numbers C语言实现

2017-03-31 19:48:34

2016-04-14 21:25:50

#### 2. Add Two Numbers LeetCode

2016-03-17 22:28:13

2016-09-26 22:56:40

#### leetcode: 2. Add Two Numbers

2017-02-26 18:29:41

#### LeetCode --- 2. Add Two Numbers

2015-01-27 22:21:25