-
Notifications
You must be signed in to change notification settings - Fork 0
/
2add_two_number.cpp
73 lines (70 loc) · 1.7 KB
/
2add_two_number.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
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if(l1 == NULL && l2 == NULL){
return NULL;
}
else if(l1 == NULL){
return l2;
}
else if(l2 == NULL){
return l1;
}
int total1 = 0, total2 = 0;
ListNode* cur1 = l1;
ListNode* cur2 = l2;
ListNode* cur;
ListNode* carry_out;
ListNode* ans;
ListNode* re_cur;
while(l1->next != NULL){
l1 = l1->next;
total1++;
}
total1++;
while(l2->next != NULL){
l2 = l2->next;
total2++;
}
total2++;
if(total1>=total2){
cur = cur1;
ans = cur1;
re_cur = cur2;
}
else if(total1<total2){
cur = cur2;
ans = cur2;
re_cur = cur1;
}
while(cur->next != NULL){
if(re_cur != NULL){
cur->val += re_cur->val;
re_cur = re_cur->next;
}
if(cur->val >= 10){
cur->val %= 10;
cur->next->val += 1;
}
cur = cur->next;
}
if(re_cur != NULL){
cur->val += re_cur->val;
}
if(cur->val>=10){
carry_out = new ListNode(1);
cur->val %= 10;
cur->next = carry_out;
}
return ans;
}
};