-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiply_Two_Linked_List.cpp
164 lines (135 loc) · 3.42 KB
/
Multiply_Two_Linked_List.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
/* Linked list Node */
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
/* Function to create a new Node with given data */
struct Node *newNode(int data) {
struct Node *new_Node = new Node(data);
return new_Node;
}
Node *reverse(Node **r) {
Node *prev = NULL;
Node *cur = *r;
Node *nxt;
while (cur != NULL) {
nxt = cur->next;
cur->next = prev;
prev = cur;
cur = nxt;
}
*r = prev;
}
/* Function to insert a Node at the beginning of the Doubly Linked List */
void push(struct Node **head_ref, int new_data) {
/* allocate Node */
struct Node *new_Node = newNode(new_data);
/* link the old list off the new Node */
new_Node->next = (*head_ref);
/* move the head to point to the new Node */
(*head_ref) = new_Node;
}
void freeList(struct Node *Node) {
struct Node *temp;
while (Node != NULL) {
temp = Node;
Node = Node->next;
delete temp;
}
}
// } Driver Code Ends
/* Linked list node structure
struct Node
{
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
};*/
/*The method multiplies
two linked lists l1 and l2
and returns their product*/
/* Multiply contents of two linked lists */
class solution {
public:
long long multiplyTwoLists(Node *first, Node *second) {
Node* p = first;
Node* q = second;
long long int l1 = 0;
long long int l2 = 0;
while(p != NULL)
{
l1 = (l1 * 10) % 1000000007;
l1 += (p->data)% 1000000007;
p = p->next;
}
while(q != NULL)
{
l2 = (l2 * 10) % 1000000007;
l2 += (q->data)% 1000000007;
q = q->next;
}
long long int ans = (l1 * l2) % 1000000007;
return ans;
}
};
//{ Driver Code Starts.
// A utility function to print a linked list
void printList(struct Node *Node) {
while (Node != NULL) {
printf("%d ", Node->data);
Node = Node->next;
}
printf("\n");
}
/* Driver program to test above function */
int main(void) {
int t;
cin >> t;
cin.ignore();
while (t--) {
struct Node *first = NULL;
struct Node *second = NULL;
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
for (int i = 0; i < arr.size(); i++) {
push(&first, arr[i]);
}
vector<int> brr;
string input2;
getline(cin, input2);
stringstream ss2(input2);
int number1;
while (ss2 >> number1) {
brr.push_back(number1);
}
for (int i = 0; i < brr.size(); i++) {
push(&second, brr[i]);
}
reverse(&first);
reverse(&second);
solution ob;
long long res = ob.multiplyTwoLists(first, second);
cout << res << endl;
cout << "~" << endl;
freeList(first);
freeList(second);
}
return 0;
}
// } Driver Code Ends