-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #138 - Daniel/Edited - 14.03.2025 #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danzang100
wants to merge
2
commits into
Dijkstra-Edu:master
Choose a base branch
from
danzang100:feature/copy-list-with-random-pointer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...Lists/#138-Copy List with Random Pointer-Medium/Explanation-Copy List Random.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Goal was to create a deep copy of a linked list with a random pointer along with the next pointer. | ||
|
||
Initial approach was to create a vector of the linked list to be able to map indices to each of the nodes. | ||
Then a hashmap to map the Node to its corresponding index. | ||
Then a vector to store the new nodes. | ||
Then a hashmap to map the indices of the new nodes to their indices. | ||
Finally the random pointer of each node would be mapped to the corresponding index of the prev list, using both hashmaps. | ||
|
||
Optimal approach is to use a single hashmap to just | ||
map old node to new node in the first traversal. | ||
Second traversal to find the next and random pointers of the new nodes based on the values of the old node keys. | ||
|
||
Interweaved approach creates new nodes and inserts them in the new list. | ||
By traversing the interweaved list, we can assign the next node of the old node's random pointer to the new node's random pointer. One more traversal would be required to separate the two. However, I haven't implemented that approach here. | ||
52 changes: 52 additions & 0 deletions
52
Linked Lists/#138-Copy List with Random Pointer-Medium/Solution-Copy List Random.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Solution { | ||
public: | ||
Node* copyRandomList(Node* head) { | ||
// brute forced using two vectors and two hashmaps. | ||
vector<Node*> v; | ||
Node* temp = head; | ||
unordered_map<int, int> umap; | ||
unordered_map<Node*, int> vmap; | ||
int k = 0; | ||
while(temp != NULL){ | ||
v.push_back(temp); | ||
vmap[temp] = k; | ||
k++; | ||
temp = temp->next; | ||
} | ||
for(int i=0; i<v.size(); i++){ | ||
umap[i] = (v[i]->random != NULL)?vmap[v[i]->random]:-1; | ||
} | ||
Node* new_head = new Node(0); | ||
Node* new_tail = new_head; | ||
vector<Node*> new_v; | ||
for(int i=0; i<v.size(); i++){ | ||
Node* x = new Node(v[i]->val); | ||
new_tail -> next = x; | ||
new_tail = new_tail -> next; | ||
new_v.push_back(x); | ||
} | ||
Node* tail = new_head -> next; | ||
for(int i=0; i<v.size(); i++){ | ||
tail->random = (umap[i] != -1)?new_v[umap[i]]: NULL; | ||
tail = tail->next; | ||
} | ||
return new_head->next; | ||
|
||
// optimized using single hashmap | ||
Node* temp = head; | ||
unordered_map<Node*, Node*> umap; | ||
while(temp != NULL){ | ||
Node* x = new Node(temp->val); | ||
umap[temp] = x; | ||
temp = temp->next; | ||
} | ||
Node* temp2 = head; | ||
while(temp2 != NULL){ | ||
umap[temp2]->next = (temp2->next)?umap[temp2->next]:NULL; | ||
umap[temp2]->random = (temp2->random)?umap[temp2->random]:NULL; | ||
temp2 = temp2->next; | ||
} | ||
return umap[head]; | ||
|
||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solution looks good, but Kindly improve the explanation.md file just like the other PR's (Markdown + Step by Step breakdown + Time and Space Complexity explanation)