-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.cpp
134 lines (124 loc) · 3.42 KB
/
AddTwoNumbers.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
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *result = nullptr;
ListNode *itr;
int temp=0;
int carry =0;
while(l1 != nullptr && l2 != nullptr) {
temp = l1->val + l2->val + carry;
ListNode *ptr = new ListNode(temp%10);
carry = temp / 10;
if(result == nullptr) {
result = ptr;
itr = result;
} else {
itr->next = ptr;
itr = itr->next;
}
l1 = l1->next;
l2 = l2->next;
}
while(l1 != nullptr) {
temp = l1->val + carry;
ListNode *ptr = new ListNode(temp%10);
carry = temp/10;
if(result == nullptr) {
result = ptr;
itr = result;
} else {
itr->next = ptr;
itr = itr->next;
}
l1 = l1 -> next;
}
while(l2 != nullptr) {
temp = l2->val + carry;
ListNode *ptr = new ListNode(temp%10);
carry = temp/10;
if(result == nullptr) {
result = ptr;
itr = result;
} else {
itr->next = ptr;
itr = itr->next;
}
l2 = l2->next;
}
if(carry > 0) {
ListNode *ptr = new ListNode(carry);
itr->next = ptr;
itr = itr->next;
}
return result;
}
};
void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}
void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}
vector<int> stringToIntegerVector(string input) {
vector<int> output;
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
stringstream ss;
ss.str(input);
string item;
char delim = ',';
while (getline(ss, item, delim)) {
output.push_back(stoi(item));
}
return output;
}
ListNode* stringToListNode(string input) {
// Generate list from the input
vector<int> list = stringToIntegerVector(input);
// Now convert that list into linked list
ListNode* dummyRoot = new ListNode(0);
ListNode* ptr = dummyRoot;
for(int item : list) {
ptr->next = new ListNode(item);
ptr = ptr->next;
}
ptr = dummyRoot->next;
delete dummyRoot;
return ptr;
}
string listNodeToString(ListNode* node) {
if (node == nullptr) {
return "[]";
}
string result;
while (node) {
result += to_string(node->val) + ", ";
node = node->next;
}
return "[" + result.substr(0, result.length() - 2) + "]";
}
int main() {
string line;
while (getline(cin, line)) {
ListNode* l1 = stringToListNode(line);
getline(cin, line);
ListNode* l2 = stringToListNode(line);
ListNode* ret = Solution().addTwoNumbers(l1, l2);
string out = listNodeToString(ret);
cout << out << endl;
}
return 0;
}