### 问题描述

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8


### 问题分析

a: (1->2)
b: (9->8>->7)


1+9 = 0

2+8 + 进位（1） = 1

7 + 进位（1）= 8

### 代码

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(l1 == NULL || l2 == NULL){
return NULL;
}
int t = l1->val+l2->val;
int carry = t/10; // 进位
ListNode* r = new ListNode(t%10);
l1 = l1->next;
l2 = l2->next;
while(l1!=NULL&&l2!=NULL) {
t = l1->val + l2->val + carry;
carry = t/10;
r->next= new ListNode(t%10);
l1 = l1->next;
l2 = l2->next;
r = r->next;
}
ListNode* rest = 0;
if(l1!=NULL) rest = l1;
if(l2!=NULL) rest = l2;
while(rest!=NULL) {
t = rest->val + carry;
carry = t/10;
r->next= new ListNode(t%10);
rest = rest->next;
r = r->next;
}
if(carry != 0) {
r->next= new ListNode(carry);
}
}
};


### 测试

int main(){
ListNode* l1;
ListNode* l2;
ListNode a1[]={
ListNode(1),ListNode(8)
};
ListNode a2[]={
ListNode(5),ListNode(4),ListNode(9)
};
l1 = &a1[0];
l2 = &a2[0];
ListNode* t1 = l1;
ListNode* t2 = l2;
for(int i=1;i<2;i++) {
t1->next = &(a1[i]);
t1 = t1->next;
}
for(int i=1;i<3;i++) {
t2->next = &(a2[i]);
t2 = t2->next;
}

Solution s;