struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { int t=0; struct ListNode* first=NULL; struct ListNode* end=NULL; while(l1||l2) { int n1= l1 ? l1->val : 0; int n2= l2 ? l2->val : 0; int sum=n1+n2+t; if(!first) { first=end=malloc(sizeof(struct ListNode)); end->val=sum%10; end->next=NULL; } else { end->next=malloc(sizeof(struct ListNode)); end->next->val=sum%10; end=end->next; } t=sum/10; if(l1) { l1=l1->next; } if(l2) { l2=l2->next; } } if(t) { end->next=malloc(sizeof(struct ListNode)); end->next->val=t; end->next->next=NULL; } return first; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)