###### 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

2016-04-14 21:25:50

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

2017-03-31 19:48:34

2016-11-21 12:05:45

2016-05-03 09:49:29

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

2017-03-21 20:11:37

2016-04-11 09:48:50

2016-06-01 16:34:44

2016-05-01 19:30:16

2016-08-19 14:12:05