Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the same value as the current node.
DSA C++
Node* clone = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using curr->next or curr->random instead of curr->val.
Trying to copy the whole node instead of just the value.
✗ Incorrect
We need to copy the value of the current node to create the clone node.
2fill in blank
mediumComplete the code to insert the cloned node right after the current node.
DSA C++
clone->next = [1];
curr->next = clone;Attempts:
3 left
💡 Hint
Common Mistakes
Setting clone->next to curr->random or nullptr.
Not updating curr->next to point to the clone.
✗ Incorrect
We set clone->next to curr->next to insert the clone node after curr.
3fill in blank
hardFix the error in setting the random pointer of the cloned node.
DSA C++
if (curr->random != nullptr) { curr->next->random = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning curr->random directly instead of its clone.
Assigning nullptr or curr->next->random incorrectly.
✗ Incorrect
The cloned node's random pointer should point to the clone of curr->random, which is curr->random->next.
4fill in blank
hardFill both blanks to restore the original list and extract the cloned list.
DSA C++
Node* original = head; Node* copy = head->next; Node* copyHead = copy; while (original != nullptr) { original->next = [1]; copy->next = [2]; original = original->next; copy = copy->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Not skipping nodes correctly, causing cycles or broken lists.
Assigning copy->next incorrectly.
✗ Incorrect
To restore the original list, original->next should skip the clone node and point to original->next->next. For the cloned list, copy->next should point to copy->next.
5fill in blank
hardFill all three blanks to complete the clone function returning the cloned list head.
DSA C++
Node* copyRandomList(Node* head) {
if (head == nullptr) return nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* clone = new Node([1]);
clone->next = curr->next;
curr->next = clone;
curr = clone->next;
}
curr = head;
while (curr != nullptr) {
if (curr->random != nullptr) {
curr->next->random = [2];
}
curr = curr->next->next;
}
curr = head;
Node* copy = head->next;
Node* copyHead = copy;
while (curr != nullptr) {
curr->next = [3];
copy->next = copy->next != nullptr ? copy->next->next : nullptr;
curr = curr->next;
copy = copy->next;
}
return copyHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Copying wrong values to clone nodes.
Setting random pointers incorrectly.
Not restoring the original list properly.
✗ Incorrect
The clone node copies curr->val. The clone's random pointer is curr->random->next. To restore the original list, curr->next should point to curr->next->next.
