-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_Prob127.cpp
More file actions
110 lines (94 loc) · 2.36 KB
/
Easy_Prob127.cpp
File metadata and controls
110 lines (94 loc) · 2.36 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
/* Let's represent an integer in a linked list format by having each node represent a digit in the number. The nodes make up the number in reversed order.
For example, the following linked list:
1 -> 2 -> 3 -> 4 -> 5
is the number 54321.
Given two linked lists in this format, return their sum in the same linked list format.
For example, given
9 -> 9
5 -> 2
return 124 (99 + 25) as:
4 -> 2 -> 1 */
#include <iostream>
#include <cmath>
class Node {
public:
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
// 1. allocate node
Node* new_node = new Node();
// 2. put in the data
new_node->data = new_data;
// 3. Make next of new node as head
new_node->next = (*head_ref);
// 4. Move the head to point to
// the new node
(*head_ref) = new_node;
}
int toValue(Node* node)
{
int value = 0;
int k=0;
while (node != NULL)
{
value = value + (node->data)*pow(10,k);
k += 1;
node = node->next;
}
return value;
}
void toNode(int value, Node** head_ref){
signed int nbInteger = 0;
int quotient = value;
while (quotient > 10){
nbInteger += 1 ;
quotient = (int) value / pow(10,nbInteger);
}
nbInteger += 1;
quotient = 0;
int dataToPush = 0 ;
for (int i=nbInteger; i>0; i--){
dataToPush = (value - quotient)/(pow(10,i-1));
quotient += dataToPush*pow(10,i-1);
push(head_ref,dataToPush);
}
}
void printList(Node *node)
{
bool begining = true;
while (node != NULL)
{
if (begining){
std::cout<<node->data;
begining = false;
} else {
std::cout<<" -> "<<node->data;
}
node = node->next;
}
std::cout<<" "<<std::endl;
}
void add(Node* head_ref_sum1, Node* head_ref_sum2, Node** head_ref_result)
{
int value1 = 0;
int value2 = 0;
value1 = toValue(head_ref_sum1);
value2 = toValue(head_ref_sum2);
int result = value1 + value2;
std::cout << value1 << " " << value2 << std::endl;
toNode(result,head_ref_result);
}
int main(int argc, char *argv[])
{
Node* head1 = NULL;
Node* head2 = NULL;
Node* headResult = NULL;
toNode(99,&head1);
toNode(25,&head2);
printList(head1);
printList(head2);
add(head1,head2, &headResult);
printList(headResult);
}