-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTwoNumbersII3.cpp
93 lines (76 loc) · 1.78 KB
/
AddTwoNumbersII3.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<iostream>
#include<stack>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int len1 = getLength(l1), len2 = getLength(l2);
ListNode *result = len1 >= len2 ? addTwoNumbers(l1, l2, len1 - len2) : addTwoNumbers(l2, l1, len2 - len1);
if(result->val >= 10) {
ListNode *head = new ListNode(result->val / 10);
result->val %= 10;
head->next = result;
result = head;
}
return result;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2, int diff) {
if(l1 == NULL || l2 == NULL) return NULL;
ListNode *result = new ListNode(0);
if(diff > 0) {
result->val = l1->val;
result->next = addTwoNumbers(l1->next, l2, diff - 1);
}else {
result->val = l1->val + l2->val;
result->next = addTwoNumbers(l1->next, l2->next, 0);
}
if(result->next) {
result->val += result->next->val / 10;
result->next->val %= 10;
}
return result;
}
int getLength(ListNode *p) {
int len = 0;
while(p) {
len++;
p = p->next;
}
return len;
}
};
int main() {
int n;
cin>>n;
ListNode *head1 = new ListNode(0);
ListNode *p = head1;
for(int i = 0; i < n; i++) {
int num;
cin>>num;
p->next = new ListNode(num);
p = p->next;
}
head1 = head1->next;
cin>>n;
ListNode *head2 = new ListNode(0);
p = head2;
for(int i = 0; i < n; i++) {
int num;
cin>>num;
p->next = new ListNode(num);
p = p->next;
}
head2 = head2->next;
Solution *solution = new Solution();
ListNode *head = solution->addTwoNumbers(head1, head2);
while(head) {
cout<<head->val<<" ";
head = head->next;
}
return 0;
}