-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path002.cpp
40 lines (36 loc) · 942 Bytes
/
002.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
38
39
40
#include <bits/stdc++.h>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (!l1) {
return l2;
} else if (!l2) {
return l1;
} else {
ListNode* sum = new ListNode(l1->val + l2->val);
sum->next = addTwoNumbers(l1->next, l2->next);
for (ListNode* ptr = sum; ptr; ) {
if (ptr->val >= 10) {
ptr->val -= 10;
if (!ptr->next) {
ptr->next = new ListNode(1);
} else {
ptr->next->val += 1;
}
}
ptr = ptr->next;
}
return sum;
}
}
};
int main() {
return 0;
}