Leetcode 2 Add Two Numbers
class Solution{
public:
ListNode* addTwoNumbers(ListNode* l1,ListNode* l2) {
ListNode* newList = new ListNode(0);
ListNode* l3 = newList;
int carry = 0;
while(l1 || l2){
int val1 = 0;
int val2 = 0;
if(l1)
val1 = l1->val;
if(l2)
val2 = l2->val;
int sum = (val1 + val2 + carry) % 10;
carry = (val1 + val2 + carry) / 10;
ListNode* newNode = new ListNode(sum);
l3->next = newNode;
l3 = l3->next;
if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}
if(carry)
l3->next = new ListNode(carry);
return newList->next;
}
};