@@ -47,6 +47,100 @@ Time complexity: O(n)
47
47
Space complexity: O(1)
48
48
*/
49
49
50
+ //METHOD1
51
+ // #include <stdio.h>
52
+ // #include <stdlib.h>
53
+
54
+ // struct node{
55
+ // int data;
56
+ // struct node *arb;
57
+ // struct node *link;
58
+ // };
59
+
60
+ // void makeList(struct node *t, int maxCounter, int mul){
61
+ // int counter = 1;
62
+ // struct node *temp = t;
63
+ // while(counter <=maxCounter){
64
+ // t->data = counter*mul;
65
+ // if(counter == maxCounter){
66
+ // t->link = NULL;
67
+ // }else{
68
+ // t->link = (struct node *)malloc(sizeof(struct node));
69
+ // }
70
+ // t = t->link;
71
+ // counter++;
72
+ // }
73
+ // counter = 1;
74
+ // t = temp;
75
+ // while(counter <=maxCounter){
76
+ // if(t && t->link && t->link->link){
77
+ // t->arb = t->link->link;
78
+ // }else if(t && t->link){
79
+ // t->arb = t->link;
80
+ // }
81
+ // else if(temp->link){
82
+ // t->arb = temp->link;
83
+ // }
84
+ // t=t->link;
85
+ // counter++;
86
+ // }
87
+ // }
88
+
89
+ // void cloneList(struct node *head, struct node *clone){
90
+ // for(;head; head=head->link, clone=clone->link){
91
+ // clone->data = head->data;
92
+ // if(head->link == NULL){
93
+ // clone->link = NULL;
94
+ // }else{
95
+ // clone->link = (struct node *)malloc(sizeof(struct node));
96
+ // }
97
+ // }
98
+ // }
99
+
100
+ // void printList(struct node *head){
101
+ // if(head){
102
+ // printf("(%d, %p) -->", head->data, head->arb);
103
+ // printList(head->link);
104
+ // }
105
+ // printf("\n");
106
+ // }
107
+
108
+ // struct node *findPtrInClone(int val, struct node *clone){
109
+ // for(;clone; clone=clone->link){
110
+ // if(clone->data == val){
111
+ // return clone;
112
+ // }
113
+ // }
114
+ // return NULL;
115
+ // }
116
+
117
+ // void assignArb(struct node *head, struct node *clone){
118
+ // int val, cloneVal;
119
+ // struct node *clonehead = clone;
120
+ // for(;head;head=head->link,clone=clone->link){
121
+ // val = head->arb->data;
122
+ // struct node *temp = findPtrInClone(val, clonehead);
123
+ // clone->arb = temp;
124
+ // }
125
+ // }
126
+
127
+ // int main(){
128
+
129
+ // struct node *head = (struct node *)malloc(sizeof(struct node));
130
+ // struct node *t = head;
131
+ // makeList(t,8,100);
132
+ // printList(head);
133
+ // struct node *clone = (struct node *)malloc(sizeof(struct node));
134
+ // cloneList(head, clone);
135
+ // assignArb(head,clone);
136
+ // printList(clone);
137
+ // }
138
+
139
+ //================================================================================================
140
+ //METHOD2: hash table to be done later
141
+ //METHOD3: coming soon
142
+
143
+ //METHOD4
50
144
#include <stdio.h>
51
145
#include <stdlib.h>
52
146
@@ -85,53 +179,57 @@ void makeList(struct node *t, int maxCounter, int mul){
85
179
}
86
180
}
87
181
88
- void cloneList (struct node * head , struct node * clone ){
89
- for (;head ; head = head -> link , clone = clone -> link ){
90
- clone -> data = head -> data ;
91
- if (head -> link == NULL ){
92
- clone -> link = NULL ;
93
- }else {
94
- clone -> link = (struct node * )malloc (sizeof (struct node ));
95
- }
182
+ void cloneList (struct node * head ){
183
+ struct node * temp = head ;
184
+ while (temp ){
185
+ struct node * clone = (struct node * )malloc (sizeof (struct node ));
186
+ clone -> data = temp -> data ;
187
+ clone -> link = temp -> link ;
188
+ temp -> link = clone ;
189
+ temp = clone -> link ;
190
+ }
191
+ }
192
+
193
+ void setRandomNode (struct node * t1 ){
194
+ while (t1 ){
195
+ t1 -> link -> arb = t1 -> arb -> link ;
196
+ t1 = t1 -> link -> link ;
96
197
}
198
+
97
199
}
98
200
99
201
void printList (struct node * head ){
100
202
if (head ){
101
- printf ("(%d, %p) -->" , head -> data , head -> arb );
203
+ printf ("(%d,%p) -->" , head -> data , head -> arb );
102
204
printList (head -> link );
103
205
}
104
206
printf ("\n" );
105
207
}
106
208
107
- struct node * findPtrInClone (int val , struct node * clone ){
108
- for (;clone ; clone = clone -> link ){
109
- if (clone -> data == val ){
110
- return clone ;
111
- }
112
- }
113
- return NULL ;
114
- }
115
-
116
- void assignArb (struct node * head , struct node * clone ){
117
- int val , cloneVal ;
118
- struct node * clonehead = clone ;
119
- for (;head ;head = head -> link ,clone = clone -> link ){
120
- val = head -> arb -> data ;
121
- struct node * temp = findPtrInClone (val , clonehead );
122
- clone -> arb = temp ;
209
+ struct node * separateClonedList (struct node * head1 , struct node * head2 ){
210
+ struct node * temp = head2 ;
211
+ for (;head1 && head2 && head1 -> link && head2 -> link ;
212
+ head1 = head1 -> link , head2 = head2 -> link ){
213
+
214
+ head1 -> link = head2 -> link ;
215
+ head2 -> link = head2 -> link -> link ;
123
216
}
217
+ head1 -> link = head2 -> link = NULL ;
218
+ return temp ;
124
219
}
125
220
126
221
int main (){
127
-
128
222
struct node * head = (struct node * )malloc (sizeof (struct node ));
129
223
struct node * t = head ;
130
224
makeList (t ,8 ,100 );
225
+ // printList(head);
226
+ cloneList (head );
227
+ // printList(head);
228
+ setRandomNode (head );
131
229
printList (head );
132
- struct node * clone = (struct node * )malloc (sizeof (struct node ));
133
- cloneList (head , clone );
134
- assignArb (head ,clone );
135
- printList (clone );
136
- }
137
230
231
+ struct node * clone = separateClonedList (head , head -> link );
232
+
233
+ printList (head );
234
+ printList (clone );
235
+ }
0 commit comments