-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClone_A_LinkedList_With_Next_And_Random_Pointer.cpp
More file actions
171 lines (150 loc) · 3.86 KB
/
Clone_A_LinkedList_With_Next_And_Random_Pointer.cpp
File metadata and controls
171 lines (150 loc) · 3.86 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
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
//{ Driver Code Starts
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
/* Link list Node */
struct Node {
int data;
Node *next;
Node *random;
Node(int x) {
data = x;
next = NULL;
random = NULL;
}
};
void print(Node *root) {
Node *temp = root;
while (temp != NULL) {
int k;
if (temp->random == NULL)
k = -1;
else
k = temp->random->data;
cout << temp->data << " " << k << " ";
temp = temp->next;
}
}
void append(Node **head_ref, Node **tail_ref, int new_data) {
Node *new_node = new Node(new_data);
if (*head_ref == NULL) {
*head_ref = new_node;
} else {
(*tail_ref)->next = new_node;
}
*tail_ref = new_node;
}
bool validation(Node *head, Node *res) {
Node *temp1 = head;
Node *temp2 = res;
while (temp1 != NULL && temp2 != NULL) {
if (temp1->data != temp2->data)
return false;
if ((temp1->random == NULL && temp2->random != NULL) ||
(temp1->random != NULL && temp2->random == NULL) ||
(temp1->random != NULL && temp2->random != NULL &&
temp1->random->data != temp2->random->data))
return false;
temp1 = temp1->next;
temp2 = temp2->next;
}
return true;
}
// } Driver Code Ends
/* Link list Node
struct Node {
int data;
Node *next;
Node *random;
Node(int x) {
data = x;
next = NULL;
random = NULL;
}
};*/
class Solution {
public:
Node *copyList(Node *head) {
Node* temp = head;
while(temp != NULL)
{
Node* copyNode = new Node(temp->data);
copyNode->next = temp->next;
temp->next = copyNode;
temp = temp->next->next;
}
temp = head;
while(temp != NULL)
{
temp->next->random = temp->random;
temp = temp->next->next;
}
temp = head;
Node* dummy = new Node(-1);
Node* res = dummy;
while(temp != NULL)
{
res->next = temp->next;
temp->next = temp->next->next;
res = res->next;
temp = temp->next;
}
return dummy->next;
}
};
//{ Driver Code Starts.
int main() {
int T;
cin >> T;
cin.ignore(); // Ignore the newline after T
while (T--) {
string input;
getline(cin, input);
stringstream ss(input);
vector<int> arr;
int number;
while (ss >> number) {
arr.push_back(number);
}
Node *head = NULL, *tail = NULL;
for (int i = 0; i < arr.size(); ++i) {
append(&head, &tail, arr[i]);
}
getline(cin, input);
stringstream ss2(input);
vector<int> arr2;
while (ss2 >> number) {
arr2.push_back(number);
}
Node *temp = head;
for (int i = 0; i < arr2.size(); i += 2) {
int a = arr2[i];
int b = arr2[i + 1];
Node *tempA = head;
Node *tempB = head;
for (int j = 1; j < a; ++j) {
tempA = tempA->next;
}
for (int j = 1; j < b; ++j) {
tempB = tempB->next;
}
tempA->random = tempB;
}
Solution ob;
Node *res = ob.copyList(head);
if (res == head) {
cout << "false" << endl;
continue;
}
if (validation(head, res)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
}
return 0;
}
// } Driver Code Ends