题目链接:戳这里
代码:
/**
* 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) {
ListNode *now;
ListNode *run = new ListNode(0);
now=run;/// now 相当于指针链表 用于存储更新的每个run元素
int more=0;
while(l1 || l2){
run->val=more;
if(l1!=NULL){
run->val+=l1->val;
l1=l1->next;
}
if(l2!=NULL){
run->val+=l2->val;
l2=l2->next;
}
more=run->val/10;
run->val%=10;
if(l1 || l2 || more>0){
run->next=new ListNode(more);
run=run->next;
}
}
return now;
}
};