You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int jin=0;
int yu=0;
ListNode* head=new ListNode(0);
head->next=l1;
ListNode *p=head;
for( ; l1 && l2 ; l1=l1->next,l2=l2->next,p=p->next){
yu=(l1->val+l2->val+jin)%10;
jin=(l1->val+l2->val+jin)/10;
l1->val=yu;
}
ListNode *q=l1 ? l1 : l2;
p->next=q;
for(; q ; q=q->next,p=p->next){
yu=(q->val+jin)%10;
jin=(q->val+jin)/10;
q->val=yu;
}
if(q==NULL && jin){
ListNode *tear=new ListNode(jin);
p->next=tear;
}
p=head->next;
delete head;
return p;
}
};