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 Javascript
let newNode = new Node(current.val[1]);Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses when calling the constructor.
Using brackets or braces instead of parentheses.
✗ Incorrect
The Node constructor requires parentheses to pass the value argument.
2fill in blank
mediumComplete the code to link the new node right after the current node.
DSA Javascript
current.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current.next to current itself causes a cycle.
Assigning current.next to null breaks the list.
✗ Incorrect
We want to insert the new node after current, so current.next should point to newNode.
3fill in blank
hardFix the error in setting the random pointer of the copied node.
DSA Javascript
current.next.random = current.random[1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting random to current.random instead of current.random.next.
Using .random.random which is incorrect.
✗ Incorrect
The copied node's random pointer should point to the copied node of current.random, which is current.random.next.
4fill in blank
hardFill both blanks to restore the original list and extract the copied list.
DSA Javascript
original.next = original.next[1]; copied.next = copied.next[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using .random instead of .next breaks the list.
Assigning empty string causes syntax errors.
✗ Incorrect
To restore the original list and copied list, we skip alternate nodes by assigning next pointers to next.next.
5fill in blank
hardFill all three blanks to complete the function that clones the linked list with random pointers.
DSA Javascript
function copyRandomList(head) {
if (!head) return null;
let current = head;
while (current) {
let newNode = new Node(current.val[1]);
newNode.next = current.next;
current.next = newNode;
current = newNode.next;
}
current = head;
while (current) {
if (current.random) {
current.next.random = current.random[2];
}
current = current.next.next;
}
let original = head;
let copy = head.next;
let copiedHead = copy;
while (original) {
original.next = original.next[3];
copy.next = copy.next ? copy.next.next : null;
original = original.next;
copy = copy.next;
}
return copiedHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Missing parentheses in Node constructor.
Setting random pointer incorrectly.
Not restoring original list properly.
✗ Incorrect
The blanks correspond to calling the Node constructor with parentheses, accessing current.random.next for the copied random pointer, and restoring original.next to original.next.next.
