Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danzang100
Copy link

Solution and Explanation uploaded.

@JRS296 JRS296 self-requested a review March 17, 2025 12:43
Copy link
Member

@JRS296 JRS296 left a 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)

Comment on lines +1 to +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.
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants