https://leetcode-cn.com/problems/sum-lists-lcci/
/**
* 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 *head = new ListNode(-1), *p1 = l1, *p2 = l2, *p = head;
int sum = 0, carr = 0;
while (p1 || p2 || carr)
{
sum = 0;
if(p1)
{
sum += (p1->val);
p1 = p1->next;
}
if(p2)
{
sum += (p2->val);
p2 = p2->next;
}
sum += carr;
ListNode *t = new ListNode(sum % 10);
carr = sum / 10;
p->next = t;
p = p->next;
}
return head->next;
}
};