Skip to content

Commit 7fe506b

Browse files
neu!
1 parent 377cc7f commit 7fe506b

File tree

1 file changed

+101
-7
lines changed

1 file changed

+101
-7
lines changed

cracking_the_coding_interview_problems/my-2-5-add-lists.cpp

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
* Finally, we will solve the follow up.
2020
*/
2121

22-
#include <iostream>
23-
#include <cmath>
22+
#define FMT_HEADER_ONLY
23+
24+
#include <bits/stdc++.h>
25+
#include <fmt/core.h>
26+
27+
using namespace std;
2428

2529
struct Node
2630
{
@@ -269,6 +273,7 @@ Node *my_add(Node *list1, Node *list2)
269273
long nbone{0}, nbtwo{0}, sum{0}, firstDig{0};
270274
Node *head = new Node(0);
271275
Node *current = head;
276+
bool mt9 = false; // true jeśli z poprzedniego dodawanie przekroczyliśmy 9
272277

273278
while (list1 || list2)
274279
{
@@ -283,16 +288,22 @@ Node *my_add(Node *list1, Node *list2)
283288
nbtwo = list2->data;
284289
list2 = list2->next;
285290
}
286-
sum = nbone + nbtwo;
291+
if (mt9)
292+
sum = current->data + nbone + nbtwo;
293+
else
294+
sum = nbone + nbtwo;
295+
287296
if (sum > 9)
288297
{
289298
firstDig = sum % 10;
290-
current->data += firstDig;
299+
current->data = firstDig;
300+
mt9 = true;
291301
my_insert(current, 1);
292302
}
293303
else
294304
{
295-
current->data += sum;
305+
current->data = sum;
306+
mt9 = false;
296307

297308
// jeśli będziemy jeszcze dodawać
298309
if (list1 || list2)
@@ -303,6 +314,89 @@ Node *my_add(Node *list1, Node *list2)
303314
return head;
304315
}
305316

317+
Node *my_add2(Node *list1, Node *list2)
318+
{
319+
long nbone{0}, nbtwo{0}, dec{0};
320+
321+
while (list1 || list2)
322+
{
323+
if (list1)
324+
{
325+
nbone += list1->data * pow(10, dec);
326+
list1 = list1->next;
327+
}
328+
if (list2)
329+
{
330+
nbtwo += list2->data * pow(10, dec);
331+
list2 = list2->next;
332+
}
333+
dec++;
334+
}
335+
336+
int sum = nbone + nbtwo;
337+
dec--;
338+
339+
Node *head = nullptr;
340+
while (dec >= 0)
341+
{
342+
Node *temp = new Node(sum / pow(10, dec));
343+
sum -= temp->data * pow(10, dec);
344+
345+
if (head)
346+
temp->next = head;
347+
head = temp;
348+
349+
dec--;
350+
}
351+
352+
return head;
353+
}
354+
355+
pair<int, int> getNumberRecur(Node *list)
356+
{
357+
if (!list)
358+
return make_pair(0, 0);
359+
360+
// decimal, sum
361+
pair<int, int> result;
362+
if (list->next)
363+
result = getNumberRecur(list->next);
364+
365+
int num = list->data * pow(10, result.first);
366+
return make_pair(result.first + 1, result.second + num);
367+
}
368+
369+
Node *my_add_recursive2(Node *list1, Node *list2)
370+
{
371+
// decimal, number
372+
pair<int, int> nbone = getNumberRecur(list1);
373+
pair<int, int> nbtwo = getNumberRecur(list2);
374+
375+
int sum = nbone.second + nbtwo.second;
376+
int dec = max(nbone.first, nbtwo.first);
377+
378+
Node *head = nullptr;
379+
Node *prev = nullptr;
380+
while (dec >= 0)
381+
{
382+
Node *temp = new Node(sum / pow(10, dec));
383+
sum -= temp->data * pow(10, dec);
384+
385+
if (!head)
386+
{
387+
head = temp;
388+
prev = temp;
389+
}
390+
391+
prev->next = temp;
392+
prev = temp;
393+
394+
dec--;
395+
}
396+
397+
return head;
398+
}
399+
306400
int main()
307401
{
308402
// making list 1 for number 617
@@ -315,7 +409,7 @@ int main()
315409

316410
// making list2 for number 295
317411
Node *list2 = nullptr;
318-
insert(list2, 2);
412+
insert(list2, 3);
319413
insert(list2, 9);
320414
insert(list2, 5);
321415
std::cout << "List2: ";
@@ -365,7 +459,7 @@ int main()
365459
list5 = add_followup(list1, list2);
366460
std::cout << "My solution\n";
367461
std::cout << "List5: ";
368-
list5 = my_add(list1, list2);
462+
list5 = my_add_recursive2(list1, list2);
369463
printList(list5);
370464

371465
deleteList(list1);

0 commit comments

Comments
 (0)