-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwo.cpp
More file actions
176 lines (150 loc) · 3.15 KB
/
addTwo.cpp
File metadata and controls
176 lines (150 loc) · 3.15 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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
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)
{
string str = getString(l1);
string revStr = getReverseString(str);
string str1 = getString(l2);
string revStr1 = getReverseString(str1);
int sum = getSum(revStr, revStr1);
string sumStr = to_string(sum);
string revSumStr = getReverseString(sumStr);
int *arr = getIntArr(revSumStr);
ListNode *resultList = createList(arr, to_string(sum).length());
return resultList;
}
private:
int getLen(ListNode *l)
{
int i = 0;
while (l && l->val)
{
l = l->next;
i++;
}
return i;
}
private:
string getString(ListNode *l)
{
string str;
while (l && l->val)
{
str.push_back(l->val + '0');
l = l->next;
}
return str;
}
private:
string getReverseString(string s)
{
int len = s.length();
string revStr;
for (int i = len - 1; i >= 0; i--)
revStr.push_back(s[i]);
return revStr;
}
private:
int getSum(string s1, string s2)
{
int num1 = stoi(s1);
int num2 = stoi(s2);
return num1 + num2;
}
private:
int *getIntArr(string s)
{
int len = s.length();
int *arr = (int *)malloc(sizeof(int) * len);
for (int i = 0; i < len; i++)
arr[i] = (int)s[i] - 48;
return arr;
}
public:
ListNode *createList(int *arr, int len)
{
ListNode *l = new ListNode();
ListNode *previous = new ListNode();
for (int i = 0; i < len; i++)
{
if (l != NULL)
{
ListNode *curr = new ListNode();
curr->val = arr[i];
curr->next = NULL;
// If first node
if (!l->val)
{
l = curr;
previous = l;
}
else
{
previous->next = curr;
previous = curr;
}
}
}
return l;
}
};
void displayList(ListNode *l)
{
while (l && l->val)
{
cout << "val: " << l->val << endl;
l = l->next;
}
}
int main()
{
int arr1[] = {2, 4, 3};
int len1 = sizeof(arr1) / sizeof(arr1[0]);
Solution sol;
ListNode *l1 = sol.createList(arr1, len1);
int arr2[] = {5, 6, 4};
int len2 = sizeof(arr2) / sizeof(arr2[0]);
ListNode *l2 = sol.createList(arr2, len2);
ListNode *result = sol.addTwoNumbers(l1, l2);
return 0;
}
// private:
// char *_getStr(ListNode *l, int length)
// {
// char *str;
// while (l && l->val)
// {
// char tempStr[1];
// tempStr[0] = (char)l->val;
// if (str[0] == '\0')
// strcpy(str, tempStr);
// else
// strcat(str, tempStr);
// l = l->next;
// }
// return str;
// }