πDay 6. Clone List with Next and Random π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
You are given a special linked list where each node has two pointers:
A
nextpointer pointing to the next node.A
randompointer 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.
π Example Walkthrough:
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
1points to2as itsnextand3as itsrandom.Node
2points to3as itsnextand1as itsrandom.Node
3points to4as itsnextand5as itsrandom.Node
4points to5as itsnextand3as itsrandom.Node
5points toNULLas itsnextand2as itsrandom.
Input:
LinkedList: 1->3->5->9
Pairs: [[1,1],[3,4]]
Output:
True
Explanation:
Node
1points to itself as itsrandom.Node
3does not have a validrandommapping in the given pairs, so it remainsNULL.
Constraints:
1 <= number of random pointers <= number of nodes <= 1000 <= node->data <= 10001 <= a, b <= 100
π― My Approach:
Steps:
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'.
Update Random Pointers:
For each cloned node, set its
randompointer to point to the cloned version of therandompointer of the original node.
Separate the Cloned List:
Extract the cloned nodes into a separate list while restoring the original list.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where
nis 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.
π Solution Code
Code (C++)
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;
}
};Code (Java)
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;
}
}Code (Python)
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π― Contribution and Support:
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! β
πVisitor Count
Last updated