Skip to content

Commit 5394ea4

Browse files
committed
Linked List Problem
1 parent 787fbd8 commit 5394ea4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Linked_List_Problem/Leetcode_138.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ Node* clone(Node *start)
5959
return temp;
6060
}
6161

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+
6280
int main()
6381
{
6482
Node* start = new Node(1);
@@ -78,7 +96,10 @@ int main()
7896
print(start);
7997
cout << "\nCloned list : \n";
8098
Node *cloned_list = clone(start);
81-
print(cloned_list);
99+
print(cloned_list);
100+
cout << "\nCloned list : \n";
101+
Node *cloned_list_1= clone_1(start);
102+
print(cloned_list_1);
82103

83104
return 0;
84105
}

Linked_List_Problem/Leetcode_138.exe

45.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)