Difficulty | Source | Tags | |
---|---|---|---|
Hard |
160 Days of Problem Solving |
|
The problem can be found at the following link: Problem Link
You are given a special linked list where each node has two pointers:
- A
next
pointer pointing to the next node. - A
random
pointer pointing to any random node in the list orNULL
.
Your task is to construct a deep copy of the linked list. The copied list should consist of the same number of nodes, and both the next
and random
pointers in the new list should be correctly set.
Input:
LinkedList: 1->2->3->4->5
Pairs: [[1,3],[2,1],[3,5],[4,3],[5,2]]
Output:
True
Explanation:
The copied linked list maintains the same structure:
- Node
1
points to2
as itsnext
and3
as itsrandom
. - Node
2
points to3
as itsnext
and1
as itsrandom
. - Node
3
points to4
as itsnext
and5
as itsrandom
. - Node
4
points to5
as itsnext
and3
as itsrandom
. - Node
5
points toNULL
as itsnext
and2
as itsrandom
.
Input:
LinkedList: 1->3->5->9
Pairs: [[1,1],[3,4]]
Output:
True
Explanation:
- Node
1
points to itself as itsrandom
. - Node
3
does not have a validrandom
mapping in the given pairs, so it remainsNULL
.
1 <= number of random pointers <= number of nodes <= 100
0 <= node->data <= 1000
1 <= a, b <= 100
-
Clone Nodes:
- Insert cloned nodes between the original nodes. For example, if the list is
1 -> 2 -> 3
, after cloning it becomes1 -> 1' -> 2 -> 2' -> 3 -> 3'
.
- Insert cloned nodes between the original nodes. For example, if the list is
-
Update Random Pointers:
- For each cloned node, set its
random
pointer to point to the cloned version of therandom
pointer of the original node.
- For each cloned node, set its
-
Separate the Cloned List:
- Extract the cloned nodes into a separate list while restoring the original list.
- Expected Time Complexity: O(n), where
n
is the number of nodes in the linked list. We iterate through the list a constant number of times. - Expected Auxiliary Space Complexity: O(1), as the algorithm uses no extra space apart from variables for iteration.
class Solution {
public:
Node* cloneLinkedList(Node* head) {
if (!head) return nullptr;
for (Node* t = head; t; t = t->next->next) {
Node* n = new Node(t->data);
n->next = t->next;
t->next = n;
}
for (Node* t = head; t; t = t->next->next)
if (t->random) t->next->random = t->random->next;
Node* newHead = head->next;
for (Node* t = head; t; t = t->next) {
Node* temp = t->next;
t->next = temp->next;
if (temp->next) temp->next = temp->next->next;
}
return newHead;
}
};
class Solution {
Node cloneLinkedList(Node head) {
if (head == null) return null;
for (Node t = head; t != null; t = t.next.next) {
Node n = new Node(t.data);
n.next = t.next;
t.next = n;
}
for (Node t = head; t != null; t = t.next.next) {
if (t.random != null) t.next.random = t.random.next;
}
Node newHead = head.next;
for (Node t = head; t != null; t = t.next) {
Node temp = t.next;
t.next = temp.next;
if (temp.next != null) temp.next = temp.next.next;
}
return newHead;
}
}
class Solution:
def cloneLinkedList(self, head):
if not head:
return None
d = {}
ch = Node(head.data)
chh = ch
d[head] = ch
h = head.next
while h:
nn = Node(h.data)
chh.next = nn
d[h] = nn
h = h.next
chh = nn
h = head
chh = ch
while h:
if h.random:
chh.random = d[h.random]
h = h.next
chh = chh.next
return ch
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐