class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* answer = l1;
ListNode* prev;
int c = 0;
while(l1 && l2)
{
int n = l1->val + l2->val + c;
if(n >= 10)
{
l1->val = n%10;
c = n / 10;
}
else
{
l1->val = n;
c = 0;
}
prev = l1;
l1 = l1->next;
l2 = l2->next;
}
while(l1)
{
int n = l1->val + c;
cout << n << "\n";
if(n >= 10)
{
l1->val = n%10;
c = n / 10;
}
else
{
l1->val = n;
c = 0;
}
prev = l1;
l1 = l1->next;
}
if(l2) prev->next = l2;
while(l2)
{
int n = l2->val + c;
cout << n << "\n";
if(n >= 10)
{
l2->val = n%10;
c = n / 10;
}
else
{
l2->val = n;
c = 0;
}
prev = l2;
l2 = l2->next;
}
if(c != 0) prev->next = new ListNode(c);
return answer;
}
};