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 Typescript
const newNode = new Node(current.val[1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or curly braces instead of parentheses.
Forgetting to add parentheses when creating a new object.
✗ Incorrect
To create a new Node instance, we call the constructor with parentheses and pass the value.
2fill in blank
mediumComplete the code to link the cloned node after the current node.
DSA Typescript
current.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current.next to itself or null.
Not linking the new node properly.
✗ Incorrect
We set current.next to the new cloned node to insert it right after the current node.
3fill in blank
hardFix the error in setting the random pointer of the cloned node.
DSA Typescript
clone.random = current.random[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using .next without checking if current.random is null.
Using .random instead of .next for the clone.
✗ Incorrect
We use optional chaining ?.next to safely get the next node of current.random if it exists.
4fill in blank
hardFill both blanks to restore the original list and extract the cloned list.
DSA Typescript
original.next = [1]; clone.next = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up original and clone pointers.
Not skipping nodes properly causing cycles.
✗ Incorrect
We restore original.next to original.next.next and clone.next to clone.next.next to separate the lists.
5fill in blank
hardFill all three blanks to complete the function that clones the linked list with random pointers.
DSA Typescript
function cloneLinkedList(head: Node | null): Node | null {
if (!head) return null;
let current = head;
while (current) {
const newNode = new Node(current.val[1]);
newNode.next = current.next;
current.next = newNode;
current = newNode[2];
}
current = head;
while (current) {
if (current.random) {
current.next.random = current.random[3];
}
current = current.next.next;
}
let original = head;
let clone = head.next;
const cloneHead = clone;
while (original) {
original.next = clone.next;
original = original.next;
if (original) {
clone.next = original.next;
clone = clone.next;
}
}
return cloneHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses when creating new nodes.
Incorrectly moving current pointer causing infinite loops.
Not using optional chaining for random pointers.
✗ Incorrect
We create new nodes with (), move current to newNode.next, and set random pointers using optional chaining ?.next.
