题解 | #链表相加(一)#
链表相加(一)
https://www.nowcoder.com/practice/521d83306d964c1188639033eb59621d
ListNode* ListAdd(ListNode* l1, ListNode* l2) {
// write code here
bool low_in = false;
ListNode* target = nullptr,*now = nullptr;
ListNode* new_l = new ListNode(0);
target = new_l,now = new_l;
int add1 = 0,add2 = 0,add3;
while(l1||l2)
{
add1=l1?l1->val:0;
add2=l2?l2->val:0;
add3=low_in?add1+add2+1:add1+add2;
low_in = add3/10;
ListNode* new_l = new ListNode(add3%10);
now->next = new_l;
now = new_l;
l1=l1?l1->next:l1;
l2=l2?l2->next:l2;
}
if(low_in)
{
ListNode* new_l = new ListNode(1);
now->next = new_l;
}
return target->next;
}
};