Copy List with Random Pointer - Complete Solution Guide
Copy List with Random Pointer is LeetCode problem 138, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null . Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the sam
Detailed Explanation
The problem requires creating a deep copy of a linked list where each node contains a 'val', a 'next' pointer, and a 'random' pointer. The 'random' pointer can point to any node in the original list or be null. A deep copy means creating entirely new nodes with the same values and linking them in the same way as the original list, without any shared nodes between the original and the copy. The function receives the head of the original linked list as input and must return the head of the new, copied linked list.
Solution Approach
The solution uses a two-pass approach and a hash table (or dictionary) to perform a deep copy of the linked list. The first pass creates all the new nodes and stores the mapping between the original nodes and the new nodes in the hash table. The second pass iterates through the original linked list again and uses the hash table to set the 'next' and 'random' pointers of the new nodes, effectively recreating the structure of the original list in the copied list.
Step-by-Step Algorithm
- Step 1: Handle the base case: If the input `head` is null, return null.
- Step 2: Create a hash table (dictionary) called `old_to_new` to store the mapping between original nodes and their copies. Initialize it with {None: None} or {null: null} to handle null random pointers.
- Step 3: Iterate through the original linked list. For each node, create a new node with the same value and store the mapping between the original node and the new node in the `old_to_new` hash table.
- Step 4: Iterate through the original linked list again. For each node, retrieve its corresponding copied node from the `old_to_new` hash table.
- Step 5: Set the `next` pointer of the copied node to the copied version of the original node's `next` pointer (using the `old_to_new` hash table).
- Step 6: Set the `random` pointer of the copied node to the copied version of the original node's `random` pointer (using the `old_to_new` hash table).
- Step 7: Return the copied version of the original head node from the `old_to_new` hash table.
Key Insights
- Insight 1: A simple iterative approach of copying node values will not suffice because the 'random' pointers need to point to the *copied* nodes, not the original ones.
- Insight 2: A hash table (or dictionary) is crucial to map original nodes to their corresponding copied nodes. This allows you to easily retrieve the copied node when setting the 'next' and 'random' pointers of the copied list.
- Insight 3: Handling the null 'random' pointers correctly is important. They should be mapped to null in the copied list.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, Linked List.
Companies
Asked at: Docusign, Flipkart, Google, Intel, Mobileye, Morgan Stanley, Nvidia, Oracle, PhonePe, Snowflake, Uber, VMware, Walmart Labs, Wix, Yahoo, eBay, oyo.