-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths.cpp
85 lines (68 loc) · 1.37 KB
/
s.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
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
#include <iostream>
using namespace std;
class Node {
public:
Node () = delete;
Node (int val) : next(NULL), value(val) {}
void insert (int value) {
Node * current = this;
while (current->next != NULL) {
current = current->next;
}
current->next = new Node (value);
return;
}
void show (void) {
Node * current = this;
while (current != NULL) {
cout << current->value << " ";
current = current->next;
}
cout << endl;
return;
}
Node * next;
int value;
};
void merge (Node * n, Node * n2) {
int first = 0;
int second = 0;
while (n != NULL) {
first += n->value;
if (n->next != NULL) {
first *= 10;
}
n = n->next;
}
while (n2 != NULL) {
second += n2->value;
if (n2->next != NULL) {
second *= 10;
}
n2 = n2->next;
}
int total = first + second;
cout << total << "\n";
return;
}
int main (void) {
Node n(5);
n.insert(1);
n.insert(8);
n.insert(5);
n.insert(4);
n.insert(1);
n.insert(9);
n.show();
Node n2(4);
n2.insert(6);
n2.insert(5);
n2.insert(2);
n2.insert(2);
n2.insert(9);
n2.insert(1);
n2.insert(2);
n2.show();
merge (&n, &n2);
return 0;
}