/**
* 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 * pHead = new ListNode(0);
ListNode * pCurrent = pHead;
int overload = 0;
while(l1 || l2 || overload)
{
int sum = ((l1 != NULL)?l1->val:0) + ((l2 != NULL)?l2->val:0);
if(overload == 1)
{
sum = sum + 1;
}
if(10 <= sum)
{
sum%=10;
overload = 1;
}
else
{
overload = 0;
}
pCurrent->next = new ListNode(sum);
pCurrent = pCurrent->next;
if(l1)
{
l1 = l1->next;
}
if(l2)
{
l2 = l2->next;
}
}
return pHead->next;
}
};