File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,24 @@ Node* clone(Node *start)
59
59
return temp;
60
60
}
61
61
62
+ Node* clone_1 (Node *head)
63
+ {
64
+ map<Node*, Node*> m;
65
+ int i=0 ;
66
+ Node* ptr = head;
67
+ while (ptr) {
68
+ m[ptr] =new Node (ptr->data );
69
+ ptr = ptr->next ;
70
+ }
71
+ ptr = head;
72
+ while (ptr) {
73
+ m[ptr]->next = m[ptr->next ];
74
+ m[ptr]->random = m[ptr->random ];
75
+ ptr = ptr->next ;
76
+ }
77
+ return m[head];
78
+ }
79
+
62
80
int main ()
63
81
{
64
82
Node* start = new Node (1 );
@@ -78,7 +96,10 @@ int main()
78
96
print (start);
79
97
cout << " \n Cloned list : \n " ;
80
98
Node *cloned_list = clone (start);
81
- print (cloned_list);
99
+ print (cloned_list);
100
+ cout << " \n Cloned list : \n " ;
101
+ Node *cloned_list_1= clone_1 (start);
102
+ print (cloned_list_1);
82
103
83
104
return 0 ;
84
105
}
You can’t perform that action at this time.
0 commit comments