迭代构建链表,没啥可说的;
具体代码:class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* newhead=new ListNode(-1); ListNode* p=newhead; int carry=0; while(l1||l2){ int sum=carry; if(l1){ sum+=l1->val; l1=l1->next; } if(l2){ sum+=l2->val; l2=l2->next; } p->next=new ListNode(sum%10); carry=sum/10; p=p->next; } if(carry!=0) p->next=new ListNode(carry); return newhead->next; }};