Skip to content

Commit b64ed2c

Browse files
authored
Merge pull request #3867 from ImmidiSivani/leetcodeque-409
solution added to 409
2 parents ede6131 + 5073c34 commit b64ed2c

File tree

5 files changed

+573
-310
lines changed

5 files changed

+573
-310
lines changed

dsa-solutions/gfg-solutions/Easy problems/Square-Root.md

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
---
22
id: square-root
33
title: Square Root
4-
sidebar_label: 9 Square Root
4+
sidebar_label: Square-Root
55
tags:
66
- Math
77
- Binary Search
8-
- Python
9-
- Java
10-
- C++
11-
- JavaScript
12-
- TypeScript
13-
description: "This document provides solutions to the problem of finding the square root of a given integer using various programming languages."
8+
description: "This document provides solutions to the problem of finding the Square Root of an integer."
149
---
1510

1611
## Problem
@@ -43,7 +38,7 @@ You don't need to read input or print anything. The task is to complete the func
4338
**Expected Auxiliary Space:** $O(1)$
4439

4540
**Constraints**
46-
- $1 ≤ x ≤ 10^7$
41+
- `1 ≤ x ≤ 10^7`
4742

4843
## Solution
4944

@@ -190,11 +185,4 @@ class Solution {
190185
The provided solutions efficiently find the floor value of the square root of a given integer `x` using binary search. This approach ensures a time complexity of $ O(log N) and an auxiliary space complexity of $O(1)$. The algorithms are designed to handle large values of `x` up to 10^7 efficiently without relying on built-in square root functions.
191186

192187
**Time Complexity:** $O(log N)$
193-
**Auxiliary Space:** $O(1)$
194-
195-
---
196-
197-
## References
198-
199-
- **GeeksforGeeks Problem:** [Square root](https://www.geeksforgeeks.org/problems/square-root/0)
200-
- **Author GeeksforGeeks Profile:** [GeeksforGeeks](https://www.geeksforgeeks.org/user/GeeksforGeeks/)
188+
**Auxiliary Space:** $O(1)$
Original file line numberDiff line numberDiff line change
@@ -1,227 +1,169 @@
11
---
2-
32
id: copy-list-with-random-pointer
4-
title: Copy List With Random Pointer
5-
level: medium
6-
sidebar_label: Copy List With Random Pointer
3+
title: Copy List with Random Pointer
4+
sidebar_label: 0138 Copy List with Random Pointer
75
tags:
8-
- Hash Table
9-
- Linked List
106
- Java
117
- Python
128
- C++
13-
description: "This document provides solutions for the Copy List With Random Pointer problem on LeetCode."
14-
9+
- JavaScript
10+
11+
description: "This is a solution to the Copy List with Random Pointer problem on LeetCode."
1512
---
1613

1714
## Problem Description
1815

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.
16+
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`.
17+
18+
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 same list state. None of the pointers in the new list should point to nodes in the original list.
2019

21-
Construct a deep copy of the list.
20+
For example, if there are two nodes `X` and `Y` in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`.
2221

2322
### Examples
2423

2524
**Example 1:**
25+
26+
![e1](https://github.com/user-attachments/assets/af16a7ff-3439-4683-8f77-9fdbb3332bef)
27+
2628
```
2729
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
2830
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
2931
```
3032

3133
**Example 2:**
34+
35+
![e2](https://github.com/user-attachments/assets/f805c77f-c6cd-4b92-9f9a-c17665bfa317)
36+
3237
```
3338
Input: head = [[1,1],[2,1]]
3439
Output: [[1,1],[2,1]]
35-
```
3640
37-
**Example 3:**
3841
```
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.
4842

4943
---
5044

51-
## Approach to Solve the Copy List with Random Pointer Problem
45+
## Solution for Copy List with Random Pointer
5246

53-
To create a deep copy of a linked list with an additional random pointer, follow these steps:
5447

55-
### Approach
48+
### Understand the Problem:
5649

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.
50+
Create a deep copy of a linked list where each node has a `next` and a `random` pointer. The new list should be identical in structure to the original, but with all new nodes. Ensure the `random` pointers in the new list accurately reflect the original's `random` pointer relationships.
5951

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`.
52+
### Approach
6253

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.
54+
1. **Interweaving Nodes**: Create and insert new nodes immediately after each original node, forming an interwoven list.
55+
2. **Assigning Random Pointers**: Set the `random` pointers of the new nodes based on the `random` pointers of the original nodes.
56+
3. **Separating Lists**: Restore the original list and extract the copied list by adjusting the `next` pointers of both original and new nodes.
6557

6658
#### Code in Different Languages
6759

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-
}
60+
<Tabs>
61+
62+
63+
<TabItem value="Python" label="Python" default>
9664

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-
}
65+
<SolutionAuthor name="sivaprasath2004"/>
10566

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-
}
67+
```python
11868

119-
return copiedHead;
120-
}
121-
};
122-
```
69+
class Node:
70+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
71+
self.val = x
72+
self.next = next
73+
self.random = random
12374

124-
### Java
125-
```java
75+
def copyRandomList(head: 'Node') -> 'Node':
76+
if not head:
77+
return None
78+
79+
current = head
80+
while current:
81+
new_node = Node(current.val, current.next, None)
82+
current.next = new_node
83+
current = new_node.next
84+
85+
current = head
86+
while current:
87+
if current.random:
88+
current.next.random = current.random.next
89+
current = current.next.next
90+
91+
original = head
92+
copy = head.next
93+
copy_head = copy
94+
95+
while original:
96+
original.next = original.next.next
97+
if copy.next:
98+
copy.next = copy.next.next
99+
original = original.next
100+
copy = copy.next
101+
102+
return copy_head
103+
104+
```
105+
</TabItem>
106+
107+
<TabItem value="Js" label="JavaScript" default>
108+
109+
<SolutionAuthor name="sivaprasath2004"/>
110+
111+
```JS
126112
class Node {
127-
int val;
128-
Node next;
129-
Node random;
130-
131-
public Node(int val) {
113+
constructor(val, next = null, random = null) {
132114
this.val = val;
133-
this.next = null;
134-
this.random = null;
115+
this.next = next;
116+
this.random = random;
135117
}
136118
}
137119

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;
120+
function copyRandomList(head) {
121+
if (!head) return null;
122+
123+
let current = head;
124+
while (current) {
125+
const newNode = new Node(current.val);
126+
newNode.next = current.next;
127+
current.next = newNode;
128+
current = newNode.next;
129+
}
130+
131+
current = head;
132+
while (current) {
133+
if (current.random) {
134+
current.next.random = current.random.next;
158135
}
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;
136+
current = current.next.next;
137+
}
138+
current = head;
139+
const newHead = head.next;
140+
let copyCurrent = newHead;
141+
142+
while (current) {
143+
current.next = current.next.next;
144+
if (copyCurrent.next) {
145+
copyCurrent.next = copyCurrent.next.next;
171146
}
172-
173-
return copiedHead;
147+
current = current.next;
148+
copyCurrent = copyCurrent.next;
174149
}
150+
151+
return newHead;
175152
}
153+
176154
```
155+
</TabItem>
156+
157+
</Tabs>
158+
177159

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
160+
### Output
185161

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-
```
162+
![Screenshot from 2024-07-19 21-11-44](https://github.com/user-attachments/assets/2c2a7efb-711d-4f6e-aebd-8f540de015c3)
219163

220164
### Complexity
221165

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.
166+
- **Time Complexity:** O(n), where `n` is the number of nodes in the linked list. The algorithm iterates through the list three times: once for interweaving nodes, once for setting random pointers, and once for separating the lists.
224167

225-
### Summary
168+
- **Space Complexity:** O(1), since the algorithm uses a constant amount of extra space beyond the input list itself (e.g., pointers for traversal and temporary variables).
226169

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)