forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 30
/
code3
171 lines (149 loc) · 3.72 KB
/
code3
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
// recursive function
Node* addition(Node* temp1, Node* temp2, int size1,
int size2)
{
// creating a new Node
Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
// base case
if (temp1->next == NULL && temp2->next == NULL) {
// addition of current nodes which is the last nodes
// of both linked lists
newNode->data = (temp1->data + temp2->data);
// set this current node's link null
newNode->next = NULL;
// return the current node
return newNode;
}
// creating a node that contains sum of previously added
// number
Node* returnedNode
= (struct Node*)malloc(sizeof(struct Node));
// if sizes are same then we move in both linked list
if (size2 == size1) {
// recursively call the function
// move ahead in both linked list
returnedNode = addition(temp1->next, temp2->next,
size1 - 1, size2 - 1);
// add the current nodes and append the carry
newNode->data = (temp1->data + temp2->data)
+ ((returnedNode->data) / 10);
}
// or else we just move in big linked list
else {
// recursively call the function
// move ahead in big linked list
returnedNode = addition(temp1, temp2->next, size1,
size2 - 1);
// add the current node and carry
newNode->data
= (temp2->data) + ((returnedNode->data) / 10);
}
// this node contains previously added numbers
// so we need to set only rightmost digit of it
returnedNode->data = (returnedNode->data) % 10;
// set the returned node to the current node
newNode->next = returnedNode;
// return the current node
return newNode;
}
// Function to add two numbers represented by nexted list.
struct Node* addTwoLists(struct Node* head1,
struct Node* head2)
{
struct Node *temp1, *temp2, *ans = NULL;
temp1 = head1;
temp2 = head2;
int size1 = 0, size2 = 0;
// calculating the size of first linked list
while (temp1 != NULL) {
temp1 = temp1->next;
size1++;
}
// calculating the size of second linked list
while (temp2 != NULL) {
temp2 = temp2->next;
size2++;
}
Node* returnedNode
= (struct Node*)malloc(sizeof(struct Node));
// traverse the bigger linked list
if (size2 > size1) {
returnedNode = addition(head1, head2, size1, size2);
}
else {
returnedNode = addition(head2, head1, size2, size1);
}
// creating new node if head node is >10
if (returnedNode->data >= 10) {
ans = (struct Node*)malloc(sizeof(struct Node));
ans->data = (returnedNode->data) / 10;
returnedNode->data = returnedNode->data % 10;
ans->next = returnedNode;
}
else
ans = returnedNode;
// return the head node of linked list that contains
// answer
return ans;
}
void Display(Node* head)
{
if (head == NULL) {
return;
}
while (head->next != NULL) {
cout << head->data << " -> ";
head = head->next;
}
cout << head->data << endl;
}
// Function that adds element at the end of the Linked List
void push(Node** head_ref, int d)
{
Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = d;
new_node->next = NULL;
if (*head_ref == NULL) {
new_node->next = *head_ref;
*head_ref = new_node;
return;
}
Node* last = *head_ref;
while (last->next != NULL && last != NULL) {
last = last->next;
}
last->next = new_node;
return;
}
// Driver Program for above Functions
int main()
{
// Creating two lists
Node* first = NULL;
Node* second = NULL;
Node* sum = NULL;
push(&first, 7);
push(&first, 5);
push(&first, 9);
push(&first, 4);
push(&first, 6);
push(&second, 8);
push(&second, 4);
cout << "First List : ";
Display(first);
cout << "Second List : ";
Display(second);
sum = addTwoLists(first, second);
cout << "Sum List : ";
Display(sum);
return 0;
}
// This code is contributed by Dharmik Parmar