Skip to content
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

Create Copy List with Random Pointer #753

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jawakarsri
Copy link

No description provided.

@jawakarsri
Copy link
Author

Time Complexity:

•	O(n) due to three passes over the list:
1.	One pass to create the interwoven list.
2.	One pass to assign the random pointers.
3.	One pass to separate the original and copied nodes.

Space Complexity:

•	O(1) since no additional space other than a few pointers is used.

@jawakarsri
Copy link
Author

Key Steps:

1.	Node Creation:
•	It iterates over the original list and creates new nodes (deep copy of each node).
•	It checks the HashMap to determine whether a new node needs to be created or reused from the map.
2.	Mapping Random Pointers:
•	For each node, the random pointer is set using the stored mapping. If the random node isn’t already in the map, it creates a copy and adds it to the map.
3.	Final List:
•	After the loop finishes, the deep copy list is fully constructed and returned.

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.

1 participant