-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClone a linked list with next and random pointer.cpp
158 lines (136 loc) · 3.35 KB
/
Clone a linked list with next and random pointer.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
//{ 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) {
// Write your code here
Node* dummy = new Node(-1);
Node* temp = head;
Node* temp2= dummy;
while(temp != NULL){
temp2->next = new Node(temp->data);
temp2 = temp2->next;
temp2->random = temp->random;
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