-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_add_two_numbers.cpp
More file actions
69 lines (61 loc) · 1.83 KB
/
2_add_two_numbers.cpp
File metadata and controls
69 lines (61 loc) · 1.83 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
/**
* Definition for singly-linked list.
* struct ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int x = 0, ListNode *next = nullptr) : val(x), next(next) {}
* };
*/
int count_list(const ListNode *list) {
int count{1};
while (list -> next) {
list = list -> next;
count += 1;
}
return count;
}
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int l1_count = count_list{l1},
l2_count = count_list{l2};
ListNode *l1_current{l1},
*l2_current{l2};
if (l1_count < l2_count) {
l1_current = l2;
l2_current = l1;
}
bool hasOverflow = false;
while (l1_count > 0 || l2_count > 0) {
if (l1_count > 0 && l2_count > 0) {
l1_current -> val += l2_current -> val;
}
if (hasOverflow) {
l1_current -> val += 1;
hasOverflow = false;
}
if (l1_current -> val >= 0x0A) {
l1_current -> val = l1_current -> val % 10;
hasOverflow = true;
}
if (l1_current -> next) {
l1_current = l1_current -> next;
}
if (l2_current -> next) {
l2_current = l2_current -> next;
}
l1_count -= 1;
l2_count -= 1;
}
if (hasOverflow) {
l1_current -> next = new ListNode;
l1_current -> next -> val = 1;
}
if (l1_count >= l2_count) {
return l1;
} else {
return l2;
}
}
};