-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddTwoNumbers.cpp
More file actions
189 lines (162 loc) · 4.17 KB
/
addTwoNumbers.cpp
File metadata and controls
189 lines (162 loc) · 4.17 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// https://leetcode.com/problems/two-sum/
//
// Status: Accepted
// Runtime: 28 ms
// Score: 99.00%
//
#include <iostream>
#include <map>
#include <vector>
#include <stack>
#include <cmath>
#include <gtest/gtest.h>
#include "args.h"
#include "macros.h"
using namespace std;
// Defined in problem
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
// Handle case when one of the numbers is null
if (!l1 && !l2)
{
cout << "somebody was null" << endl;
return nullptr;
}
ListNode *answer = nullptr;
ListNode *ln = nullptr;
int carryover = 0;
// go through and add numbers, linked lists may not be the same length
do
{
int sum = carryover;
if (l1)
{
sum += l1->val;
}
if (l2)
{
sum += l2->val;
}
int remainder = sum % 10;
carryover = sum / 10;
ListNode *node = new ListNode(remainder);
if (answer == nullptr)
{
answer = node;
ln = node;
}
else
{
ln->next = node;
ln = node;
}
if (l1)
{
l1 = l1->next;
}
if (l2)
{
l2 = l2->next;
}
} while (l1 || l2);
// Finally, it is possible the answer may be longer than either of the original
// linked lists, as indicated by carryover, so add that if carryover is not 0
if (carryover)
{
ListNode *node = new ListNode(carryover);
ln->next = node;
}
return answer;
}
};
namespace addTwoNumbers
{
namespace test
{
// Create a new list node, don't forget to free
ListNode *create(int number)
{
ListNode *first = nullptr;
ListNode *prev = nullptr;
do
{
ListNode *ln = new ListNode(number % 10);
number /= 10;
if (first == nullptr)
{
first = ln;
}
if (prev != nullptr)
{
prev->next = ln;
}
prev = ln;
} while (number);
return first;
}
// Turn the ListNode back into an int
int unpack(ListNode *listNode)
{
if (listNode == nullptr)
{
throw "unpack: nullptr passed in";
}
int number = 0;
int i = 0;
ListNode *ln = listNode;
while (ln != nullptr)
{
number += (ln->val * pow(10, i));
ln = ln->next;
i++;
}
return number;
}
// Free the allocated memory
void free(ListNode *ln)
{
if (ln == nullptr)
{
throw "unpack: nullptr passed in";
}
while (ln != nullptr)
{
ListNode *next = ln->next;
delete ln;
ln = next;
}
}
}; // namespace test
}; // namespace addTwoNumbers
TEST(AddTwoNumbersTest, CorrectSample)
{
const int firstNumber = 342;
const int secondNumber = 465;
ListNode *first = addTwoNumbers::test::create(firstNumber);
ListNode *second = addTwoNumbers::test::create(secondNumber);
Solution solution;
int answerInteger = 0;
int lookingFor = firstNumber + secondNumber;
try
{
ListNode *answer = solution.addTwoNumbers(first, second);
answerInteger = addTwoNumbers::test::unpack(answer);
addTwoNumbers::test::free(answer);
addTwoNumbers::test::free(first);
addTwoNumbers::test::free(second);
}
catch (...)
{
cout << "TwoSumsTest.CorrectSample:: Caught exception" << endl;
}
ASSERT_EQ(answerInteger, lookingFor);
}