-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathaddNumbers.cpp
58 lines (51 loc) · 1.44 KB
/
addNumbers.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
#include "basicActions.hpp"
using namespace std;
Node *addLists(Node *head1, Node *head2) {
int carry = 0, sum = 0;
// Two Node pointers pointing to each of the list's head.
Node *p = head1, *q = head2;
// New list to store result.
Node *result = nullptr;
while (p || q) {
sum = 0;
// If 'p' Node exits, add it to sum and move forward.
if (p) {
sum += p->data;
p = p->next;
}
// If 'q' Node exits, add it to sum and move forward.
if (q) {
sum += q->data;
q = q->next;
}
// If there's 'carry', add 'carry' to 'sum' and reset 'carry' to 0.
if (carry != 0) {
sum += carry;
carry = 0;
}
// If 'sum' is greater than or equal to 10, add one to 'carry' and reduce 'sum' by 10.
if (sum >= 10) {
carry = 1;
sum -= 10;
}
// Insert 'sum' to a new list.
insertAtEnd(result, sum);
}
// Return the newly made list.
return result;
}
int main() {
Node *head1 = nullptr;
Node *head2 = nullptr;
// 7 -> 5 -> 9 -> 4 -> 6 (Represents 64957)
insertAtEnd(head1, 7);
insertAtEnd(head1, 5);
insertAtEnd(head1, 9);
insertAtEnd(head1, 4);
insertAtEnd(head1, 6);
// 8 -> 4 (Represents 48)
insertAtEnd(head2, 8);
insertAtEnd(head2, 4);
head1 = addLists(head1, head2);
displayNodes(head1);
}