/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
ListNode *addLists(ListNode *l1, ListNode *l2) {
// write your code here
ListNode *newhead = new ListNode(0);
ListNode *p = newhead;
int c = 0;
while (l1 || l2 || c) {
if (l1) {
c += l1?l1->val:0;
l1 = l1->next;
}
if (l2) {
c += l2?l2->val:0;
l2 = l2->next;
}
// p->val = c;
p->next = new ListNode(c);
c /= 10;
p = p->next;
}
return newhead->next;
}
};