/**
* 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) {
ListNode*pre = new ListNode(0);
ListNode*cur = pre;
int carry = 0;
while(l1||l2) {
int x = l1==NULL ? 0 : l1->val;//如果链表为空x=0 否则等于 lh结点值
int y = l2 == NULL ? 0 : l2->val;//如果链表为空y=0 否则等于 lh结点值
int sum = x + y + carry;
carry = sum / 10;
sum = sum % 10;
cur->next = new ListNode(sum);
cur = cur->next;
if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}
if(carry == 1) {
cur->next = new ListNode(carry);
}
return pre->next;
}
};