-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbersII.cpp
More file actions
104 lines (85 loc) · 1.55 KB
/
AddTwoNumbersII.cpp
File metadata and controls
104 lines (85 loc) · 1.55 KB
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
94
95
96
97
98
99
100
101
102
103
104
#include<iostream>
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 = 0, len2 = 0;
ListNode *p;
p = l1;
while(p) {
len1++;
p = p->next;
}
p = l2;
while(p) {
len2++;
p = p->next;
}
if(len2 > len1) {
ListNode *t = l1;
l1 = l2;
l2 = t;
swap(len1, len2);
}
p = l1;
for(int i = 0; i < len1 - len2; i++) p = p->next;
ListNode *q = l2;
while(p) {
p->val += q->val;
p = p->next;
q = q->next;
}
for(int i = 1; i < len1; i++) {
p = l1;
for(int j = 0; j < len1 - i; j++) {
if(p->next->val >= 10) {
p->val += p->next->val / 10;
p->next->val %= 10;
}
p = p->next;
}
}
if(l1->val >= 10) {
ListNode *t = new ListNode(l1->val / 10);
l1->val %= 10;
t->next = l1;
l1 = t;
}
return l1;
}
};
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;
}