1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode*p1=l1,*p2=l2,*p3,*last=nullptr; int up=0; ListNode* root=new ListNode(); p3=root; while(p1 || p2 || up){ int n1=0,n2=0; if(p1){n1=p1->val;p1=p1->next;} if(p2){n2=p2->val;p2=p2->next;} up+=n1+n2; p3->val=up%10; last=p3; p3=p3->next=new ListNode(); up/=10; } delete last->next; last->next=nullptr; return root; } };
|