forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
37 lines (32 loc) · 956 Bytes
/
Solution.cpp
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
33
34
35
36
37
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *ans_l = new ListNode(0);
ListNode *head = ans_l;
int tmp = 0;
while(l1 != NULL && l2 != NULL){
tmp += l1->val + l2->val;
ans_l->next = new ListNode(tmp % 10);
tmp = tmp / 10;
ans_l = ans_l->next;
l1 = l1->next;
l2 = l2->next;
}
while(l1 != NULL){
tmp += l1->val;
ans_l->next = new ListNode(tmp % 10);
tmp = tmp / 10;
ans_l = ans_l->next;
l1 = l1->next;
}
while(l2 != NULL){
tmp += l2->val;
ans_l->next = new ListNode(tmp % 10);
tmp = tmp / 10;
ans_l = ans_l->next;
l2 = l2->next;
}
if(tmp)ans_l->next = new ListNode(tmp);
return head->next;
}
};