/**
给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null
最后这四个题实在不会做了,抄的别人的
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addLists(ListNode *l1, ListNode *l2) {
// write your code here
ListNode* pre=new ListNode(0);
pre->next=solve(pre->next,l1,l2,0);
return pre->next;
}
ListNode* solve(ListNode *rt,ListNode *l1, ListNode *l2,int flag){
if(!l1&&!l2&&flag==0)return NULL;
if(l1)flag+=l1->val,l1=l1->next;
if(l2)flag+=l2->val,l2=l2->next;
rt=new ListNode(flag);
flag/=10;
if(l1||l2||flag)rt->next=solve(rt->next,l1,l2,flag);
return rt;
}
};