Skip to content

Commit a06ed85

Browse files
my iterative
1 parent 22bc7cd commit a06ed85

File tree

1 file changed

+376
-0
lines changed

1 file changed

+376
-0
lines changed
Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
/**
2+
* Cracking the coding interview edition 6
3+
* Problem 2.5 Sum lists
4+
* You have two numbers represented by linked list, where each node contains
5+
* a single digit. Digits are stored in reverse order.(1's digit is at the head)
6+
* Write a function that adds two such numbers and returns a number in similar
7+
* list format.
8+
* example:
9+
* 7-->1-->6 + 5-->9-->2 = 2-->1-->9
10+
* which is (617 + 295 = 912)
11+
* What if digits are not stored in reverse order(i.e 1's digit is at tail)
12+
* (6--1-->7) + (2-->9-->5) = (9-->1-->2)
13+
*
14+
* Approach:
15+
* We will solve the problem recursively and iteratively.
16+
* Add numbers at same digits place, store the 1's digit of the output in new list
17+
* and add carry in next place's addition.
18+
*
19+
* Finally, we will solve the follow up.
20+
*/
21+
22+
#include <iostream>
23+
#include <cmath>
24+
25+
struct Node
26+
{
27+
int data;
28+
Node *next;
29+
Node(int d) : data{d}, next{nullptr} {}
30+
};
31+
32+
/**
33+
* [insert - insert a new node at head of the list]
34+
* @param head [head of the list]
35+
* @param data [new node's data]
36+
*/
37+
void insert(Node *&head, int data)
38+
{
39+
Node *newNode = new Node(data);
40+
newNode->next = head;
41+
head = newNode;
42+
}
43+
44+
void my_insert(Node *&current, int data)
45+
{
46+
Node *newNode = new Node(data);
47+
current->next = newNode;
48+
current = newNode;
49+
}
50+
51+
/**
52+
* [printList - print the list]
53+
* @param head [head of the list]
54+
*/
55+
void printList(Node *head)
56+
{
57+
while (head)
58+
{
59+
std::cout << head->data << "-->";
60+
head = head->next;
61+
}
62+
std::cout << "nullptr" << std::endl;
63+
}
64+
65+
/**
66+
* [add_iterative iterative approach to add two given lists]
67+
* @param list1
68+
* @param list2
69+
* @return list3
70+
*/
71+
Node *add_iterative(Node *list1, Node *list2)
72+
{
73+
if (list1 == nullptr)
74+
{
75+
return list2;
76+
}
77+
if (list2 == nullptr)
78+
{
79+
return list1;
80+
}
81+
82+
// list3 will store result
83+
Node *list3 = nullptr;
84+
// for adding new nodes to tail of list3
85+
Node *list3Tail = nullptr;
86+
87+
int value = 0, carry = 0;
88+
89+
while (list1 || list2)
90+
{
91+
// add the values, if one of the list has already been traversed, add 0
92+
value = carry + (list1 ? list1->data : 0) + (list2 ? list2->data : 0);
93+
94+
// get the new value and carry
95+
if (value > 9)
96+
{
97+
carry = 1;
98+
value = value % 10;
99+
}
100+
else
101+
{
102+
carry = 0;
103+
}
104+
105+
// new node
106+
Node *temp = new Node(value);
107+
108+
// if this is the first node, populate the result, else add to the tail
109+
if (list3 == nullptr)
110+
{
111+
list3 = temp;
112+
}
113+
else
114+
{
115+
list3Tail->next = temp;
116+
}
117+
118+
// make new tail
119+
list3Tail = temp;
120+
121+
if (list1)
122+
{
123+
list1 = list1->next;
124+
}
125+
126+
if (list2)
127+
{
128+
list2 = list2->next;
129+
}
130+
}
131+
132+
if (carry > 0)
133+
{
134+
list3Tail->next = new Node(carry);
135+
}
136+
return list3;
137+
}
138+
139+
/**
140+
* [add_recursive - recursive addititon of two lists
141+
* @param list1
142+
* @param list2
143+
* @param carry
144+
* @return list3
145+
*/
146+
Node *add_recursive(Node *list1, Node *list2, int carry)
147+
{
148+
if (list1 == nullptr && list2 == nullptr && carry == 0)
149+
{
150+
return nullptr;
151+
}
152+
int value = carry;
153+
if (list1)
154+
{
155+
value += list1->data;
156+
}
157+
if (list2)
158+
{
159+
value += list2->data;
160+
}
161+
162+
Node *resultNode = new Node(value % 10);
163+
164+
resultNode->next = add_recursive(list1 ? (list1->next) : nullptr,
165+
list2 ? (list2->next) : nullptr,
166+
value > 9 ? 1 : 0);
167+
return resultNode;
168+
}
169+
170+
/**
171+
* Follow up part:
172+
* Lists are stored such that 1's digit is at the tail of list.
173+
* 617 ==> 6 --> 1 --> 7
174+
* 295 ==> 2 --> 9 --> 5
175+
*/
176+
177+
/**
178+
* [padList - Helper routine for padding the shorter list]
179+
* @param list [Current list]
180+
* @param padding [number of padding required]
181+
*/
182+
void padList(Node *&list, int padding)
183+
{
184+
for (int i = 0; i < padding; ++i)
185+
{
186+
insert(list, 0);
187+
}
188+
}
189+
190+
/**
191+
* [length - helper routine to return length of list]
192+
* @param head [list's head]
193+
* @return length of the list
194+
*/
195+
int length(Node *head)
196+
{
197+
int len = 0;
198+
while (head)
199+
{
200+
len++;
201+
head = head->next;
202+
}
203+
return len;
204+
}
205+
206+
Node *add_followup_helper(Node *list1, Node *list2, int &carry)
207+
{
208+
if (list1 == nullptr && list2 == nullptr && carry == 0)
209+
{
210+
return nullptr;
211+
}
212+
213+
Node *result = add_followup_helper(list1 ? (list1->next) : nullptr,
214+
list2 ? (list2->next) : nullptr,
215+
carry);
216+
217+
int value = carry + (list1 ? list1->data : 0) + (list2 ? list2->data : 0);
218+
insert(result, value % 10);
219+
carry = (value > 9) ? 1 : 0;
220+
return result;
221+
}
222+
223+
/**
224+
* [add_followup - adding list such that 1's digit is at tail( follow up part of question)
225+
* @param list1
226+
* @param list2
227+
* @return list3 representing sum of list1 and list2
228+
*/
229+
Node *add_followup(Node *list1, Node *list2)
230+
{
231+
int len1 = length(list1);
232+
int len2 = length(list2);
233+
234+
// pad the smaller list
235+
if (len1 > len2)
236+
{
237+
padList(list2, len1 - len2);
238+
}
239+
else
240+
{
241+
padList(list1, len2 - len1);
242+
}
243+
int carry = 0;
244+
Node *list3 = add_followup_helper(list1, list2, carry);
245+
if (carry)
246+
{
247+
insert(list3, carry);
248+
}
249+
return list3;
250+
}
251+
252+
/**
253+
* [deleteList Helper routine to delete list]
254+
* @param head [head of the list]
255+
*/
256+
void deleteList(Node *&head)
257+
{
258+
Node *nextNode;
259+
while (head)
260+
{
261+
nextNode = head->next;
262+
delete (head);
263+
head = nextNode;
264+
}
265+
}
266+
267+
Node *my_add(Node *list1, Node *list2)
268+
{
269+
long nbone{0}, nbtwo{0}, sum{0}, firstDig{0};
270+
Node *head = new Node(0);
271+
Node *current = head;
272+
273+
while (list1 || list2)
274+
{
275+
nbone = 0, nbtwo = 0;
276+
if (list1)
277+
{
278+
nbone = list1->data;
279+
list1 = list1->next;
280+
}
281+
if (list2)
282+
{
283+
nbtwo = list2->data;
284+
list2 = list2->next;
285+
}
286+
sum = nbone + nbtwo;
287+
if (sum > 9)
288+
{
289+
firstDig = sum % 10;
290+
current->data += firstDig;
291+
my_insert(current, 1);
292+
}
293+
else
294+
{
295+
current->data += sum;
296+
297+
// jeśli będziemy jeszcze dodawać
298+
if (list1 || list2)
299+
my_insert(current, 0);
300+
}
301+
}
302+
303+
return head;
304+
}
305+
306+
int main()
307+
{
308+
// making list 1 for number 617
309+
Node *list1 = nullptr;
310+
insert(list1, 6);
311+
insert(list1, 1);
312+
insert(list1, 7);
313+
std::cout << "List1: ";
314+
printList(list1);
315+
316+
// making list2 for number 295
317+
Node *list2 = nullptr;
318+
insert(list2, 2);
319+
insert(list2, 9);
320+
insert(list2, 5);
321+
std::cout << "List2: ";
322+
printList(list2);
323+
324+
Node *list3 = add_iterative(list1, list2);
325+
std::cout << "Iterative Solution: \n";
326+
std::cout << "List3: ";
327+
printList(list3);
328+
329+
Node *list4 = add_recursive(list1, list2, 0);
330+
std::cout << "Recursive Solution: \n";
331+
std::cout << "List4: ";
332+
printList(list4);
333+
334+
Node *list5 = my_add(list1, list2);
335+
std::cout << "My Solution: \n";
336+
std::cout << "List5: ";
337+
printList(list5);
338+
339+
deleteList(list1);
340+
deleteList(list2);
341+
deleteList(list3);
342+
deleteList(list4);
343+
deleteList(list5);
344+
345+
std::cout << "\n\nNow follow up case, lists are stored such that 1's digit is at the tail of list\n";
346+
// Node * listx = nullptr;
347+
insert(list1, 4);
348+
insert(list1, 3);
349+
insert(list1, 2);
350+
insert(list1, 9);
351+
std::cout << "List1: ";
352+
printList(list1);
353+
354+
insert(list2, 9);
355+
insert(list2, 9);
356+
insert(list2, 8);
357+
std::cout << "List2: ";
358+
printList(list2);
359+
360+
list3 = add_followup(list1, list2);
361+
std::cout << "Adding two above lists\n";
362+
std::cout << "List3: ";
363+
printList(list3);
364+
365+
list5 = add_followup(list1, list2);
366+
std::cout << "My solution\n";
367+
std::cout << "List5: ";
368+
list5 = my_add(list1, list2);
369+
printList(list5);
370+
371+
deleteList(list1);
372+
deleteList(list2);
373+
deleteList(list3);
374+
375+
return 0;
376+
}

0 commit comments

Comments
 (0)