思路
同时遍历两个链表,顺序加和进位
/**
* 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) {
int t=0;
ListNode *head=new ListNode(0);
ListNode *p=head;
while(l1!=NULL || l2!=NULL || t!=0){
int first=0,second=0;
if(l1!=NULL){
first=l1->val;l1=l1->next;
}
if(l2!=NULL){
second=l2->val;l2=l2->next;
}
p->next=new ListNode((first+second+t)%10);p=p->next;
t=(first+second+t)/10;
}
return head->next;
}
};