-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add_Number_Linked_Lists.cpp
213 lines (177 loc) · 4.53 KB
/
Add_Number_Linked_Lists.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//{ Driver Code Starts
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
/* Linked list Node */
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
Node* buildList() {
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
if (arr.empty()) {
return NULL;
}
int val = arr[0];
int size = arr.size();
Node* head = new Node(val);
Node* tail = head;
for (int i = 1; i < size; i++) {
val = arr[i];
tail->next = new Node(val);
tail = tail->next;
}
return head;
}
void printList(Node* n) {
while (n) {
cout << n->data << " ";
n = n->next;
}
cout << endl;
}
// } Driver Code Ends
/* node for linked list:
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
*/
class Solution {
public:
// Function to add two numbers represented by linked list.
void reverse(Node* &head)
{
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while(current)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
Node* addTwoLists(Node* num1, Node* num2) {
if(num1 == NULL) return num2;
if(num2 == NULL) return num1;
reverse(num1);
reverse(num2);
Node* head = nullptr;
Node* dummy = nullptr;
int sum = 0;
int carry = 0;
while(num1 != NULL && num2 != NULL)
{
sum = num1->data + num2->data + carry;
carry = 0;
if(sum > 9)
{
int Remainder = sum % 10;
Node * temp = new Node(Remainder);
carry = 1;
if(head == nullptr)
{
head = temp;
dummy = temp;
}
else
{
dummy->next = temp;
dummy = dummy->next;
}
}
else
{
Node* temp = new Node(sum);
if(head == nullptr)
{
head = temp;
dummy = temp;
}
else
{
dummy->next = temp;
dummy = dummy->next;
}
}
num1 = num1->next;
num2 = num2->next;
}
while(num1 != NULL)
{
sum = num1->data + carry;
carry = 0;
Node * temp = nullptr;
if(sum > 9)
{
int Remainder = sum % 10;
temp = new Node(Remainder);
carry = 1;
}
else temp = new Node(sum);
dummy->next = temp;
dummy = dummy->next;
num1 = num1->next;
}
while(num2 != NULL)
{
sum = num2->data + carry;
carry = 0;
Node* temp = nullptr;
if(sum > 9)
{
int Remainder = sum % 10;
temp = new Node(Remainder);
carry = 1;
}
else temp = new Node(sum);
dummy->next = temp;
dummy = dummy->next;
num2 = num2->next;
}
if(carry != 0)
{
Node * temp = new Node(carry);
dummy->next = temp;
dummy = dummy->next;
}
reverse(head);
while(head->data == 0) head = head->next;
return head;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore(); // To ignore the newline character after the integer input
while (t--) {
Node* num1 = buildList();
Node* num2 = buildList();
Solution ob;
Node* res = ob.addTwoLists(num1, num2);
printList(res);
}
return 0;
}
// } Driver Code Ends