Skip to content

Commit ed9544e

Browse files
authored
Merge pull request #3597 from kashvi7440/main
Made the necessary changes
2 parents 9f94b86 + d1e5b6b commit ed9544e

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
---
2+
3+
id: copy-list-with-random-pointer
4+
title: Copy List With Random Pointer
5+
level: medium
6+
sidebar_label: Copy List With Random Pointer
7+
tags:
8+
- Hash Table
9+
- Linked List
10+
- Java
11+
- Python
12+
- C++
13+
description: "This document provides solutions for the Copy List With Random Pointer problem on LeetCode."
14+
15+
---
16+
17+
## Problem Description
18+
19+
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
20+
21+
Construct a deep copy of the list.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
```
27+
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
28+
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
29+
```
30+
31+
**Example 2:**
32+
```
33+
Input: head = [[1,1],[2,1]]
34+
Output: [[1,1],[2,1]]
35+
```
36+
37+
**Example 3:**
38+
```
39+
Input: head = [[3,null],[3,0],[3,null]]
40+
Output: [[3,null],[3,0],[3,null]]
41+
```
42+
43+
### Constraints:
44+
45+
- The number of nodes in the list is in the range [0, 1000].
46+
- `-10000 <= Node.val <= 10000`
47+
- Node.random is null or is pointing to a node in the linked list.
48+
49+
---
50+
51+
## Approach to Solve the Copy List with Random Pointer Problem
52+
53+
To create a deep copy of a linked list with an additional random pointer, follow these steps:
54+
55+
### Approach
56+
57+
1. **Create Clones Adjacent to Original Nodes:**
58+
- Iterate through the original list and create a new node for each original node. Insert this new node right next to the original node. This way, each original node will have its clone right next to it.
59+
60+
2. **Assign Random Pointers to Cloned Nodes:**
61+
- Iterate through the list again. For each original node, if it has a random pointer, set the random pointer of the clone node to point to the clone of the node that the original node’s random pointer is pointing to. This can be achieved because the clone of any node `A` is next to `A`.
62+
63+
3. **Restore the Original List and Extract the Cloned List:**
64+
- Iterate through the list once more to restore the original list by separating the original nodes from their clones. Extract the cloned list by linking the cloned nodes together.
65+
66+
#### Code in Different Languages
67+
68+
### C++
69+
```cpp
70+
class Node {
71+
public:
72+
int val;
73+
Node* next;
74+
Node* random;
75+
76+
Node(int _val) {
77+
val = _val;
78+
next = NULL;
79+
random = NULL;
80+
}
81+
};
82+
83+
class Solution {
84+
public:
85+
Node* copyRandomList(Node* head) {
86+
if (!head) return nullptr;
87+
88+
// Step 1: Create a new node for each original node and insert it next to the original node.
89+
Node* curr = head;
90+
while (curr) {
91+
Node* newNode = new Node(curr->val);
92+
newNode->next = curr->next;
93+
curr->next = newNode;
94+
curr = newNode->next;
95+
}
96+
97+
// Step 2: Assign random pointers for the new nodes.
98+
curr = head;
99+
while (curr) {
100+
if (curr->random) {
101+
curr->next->random = curr->random->next;
102+
}
103+
curr = curr->next->next;
104+
}
105+
106+
// Step 3: Restore the original list and extract the copied list.
107+
curr = head;
108+
Node* copiedHead = head->next;
109+
Node* copiedCurr = copiedHead;
110+
while (curr) {
111+
curr->next = curr->next->next;
112+
if (copiedCurr->next) {
113+
copiedCurr->next = copiedCurr->next->next;
114+
}
115+
curr = curr->next;
116+
copiedCurr = copiedCurr->next;
117+
}
118+
119+
return copiedHead;
120+
}
121+
};
122+
```
123+
124+
### Java
125+
```java
126+
class Node {
127+
int val;
128+
Node next;
129+
Node random;
130+
131+
public Node(int val) {
132+
this.val = val;
133+
this.next = null;
134+
this.random = null;
135+
}
136+
}
137+
138+
class Solution {
139+
public Node copyRandomList(Node head) {
140+
if (head == null) return null;
141+
142+
// Step 1: Create a new node for each original node and insert it next to the original node.
143+
Node curr = head;
144+
while (curr != null) {
145+
Node newNode = new Node(curr.val);
146+
newNode.next = curr.next;
147+
curr.next = newNode;
148+
curr = newNode.next;
149+
}
150+
151+
// Step 2: Assign random pointers for the new nodes.
152+
curr = head;
153+
while (curr != null) {
154+
if (curr.random != null) {
155+
curr.next.random = curr.random.next;
156+
}
157+
curr = curr.next.next;
158+
}
159+
160+
// Step 3: Restore the original list and extract the copied list.
161+
curr = head;
162+
Node copiedHead = head.next;
163+
Node copiedCurr = copiedHead;
164+
while (curr != null) {
165+
curr.next = curr.next.next;
166+
if (copiedCurr.next != null) {
167+
copiedCurr.next = copiedCurr.next.next;
168+
}
169+
curr = curr.next;
170+
copiedCurr = copiedCurr.next;
171+
}
172+
173+
return copiedHead;
174+
}
175+
}
176+
```
177+
178+
### Python
179+
```python
180+
class Node:
181+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
182+
self.val = x
183+
self.next = next
184+
self.random = random
185+
186+
class Solution:
187+
def copyRandomList(self, head: 'Node') -> 'Node':
188+
if not head:
189+
return None
190+
191+
# Step 1: Create a new node for each original node and insert it next to the original node.
192+
curr = head
193+
while curr:
194+
newNode = Node(curr.val)
195+
newNode.next = curr.next
196+
curr.next = newNode
197+
curr = newNode.next
198+
199+
# Step 2: Assign random pointers for the new nodes.
200+
curr = head
201+
while curr:
202+
if curr.random:
203+
curr.next.random = curr.random.next
204+
curr = curr.next.next
205+
206+
# Step 3: Restore the original list and extract the copied list.
207+
curr = head
208+
copiedHead = head.next
209+
copiedCurr = copiedHead
210+
while curr:
211+
curr.next = curr.next.next
212+
if copiedCurr.next:
213+
copiedCurr.next = copiedCurr.next.next
214+
curr = curr.next
215+
copiedCurr = copiedCurr.next
216+
217+
return copiedHead
218+
```
219+
220+
### Complexity
221+
222+
- **Time Complexity:** $O(n)$ - Each of the three steps involves a single pass through the list.
223+
- **Space Complexity:** $O(1)$ - The space complexity is constant as we are not using any additional data structures for storage.
224+
225+
### Summary
226+
227+
This approach efficiently creates a deep copy of a linked list with random pointers by leveraging the existing structure of the list and ensuring that each node and its clone are linked adjacently.

0 commit comments

Comments
 (0)