/**
* 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 carry = 0;
ListNode* tmpl1 = l1, *tmpl2 = l2;
ListNode dummy(-1);
ListNode* tmprec = &dummy;
// cout << "hello" << endl;
//return NULL;
while(tmpl1 != NULL && tmpl2 != NULL){
int res = carry + tmpl1->val + tmpl2->val;
carry = res / 10;
res %= 10;
tmpl1 = tmpl1->next;
tmpl2 = tmpl2->next;
tmprec->next = new ListNode(res);
tmprec = tmprec->next;
}
while(tmpl1!=NULL){
int res = carry + tmpl1->val;
carry = res / 10;
res %= 10;
tmpl1 = tmpl1->next;
tmprec->next = new ListNode(res);
tmprec = tmprec->next;
}
while(tmpl2 != NULL){
int res = carry + tmpl2->val;
carry = res / 10;
res %= 10;
tmpl2 = tmpl2->next;
tmprec->next = new ListNode(res);
tmprec = tmprec->next;
}
while(carry > 0){
ListNode* tmpptr = new ListNode(carry % 10);
//carry /= 10;
tmprec->next = new ListNode(carry % 10);
carry /= 10;
tmprec = tmprec->next;
}
return dummy.next;
}
};
错误1:
每次处理余下的位和进位的问题
错误2:
第一次的tmprec初始化为&dummy