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 copy = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next or current.random instead of current.val.
Passing null instead of the node's value.
✗ Incorrect
We create a new node copying the value of the current node using current.val.
2fill in blank
mediumComplete the code to link the copied node after the current node.
DSA C#
copy.next = current.[1];
current.next = copy;Attempts:
3 left
💡 Hint
Common Mistakes
Using random instead of next.
Using val or prev which are not pointers.
✗ Incorrect
We set copy.next to current.next to insert the copy right after current.
3fill in blank
hardFix the error in setting the random pointer of the copied node.
DSA C#
if (current.random != null) { current.next.random = current.random.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using random instead of next for the copied node.
Using val or prev which are not pointers.
✗ Incorrect
The copied node's random should point to the copy 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 C#
original.next = original.next.[1]; copied.next = copied.next == null ? null : copied.next.[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using random or val instead of next.
Not checking for null before accessing copied.next.next.
✗ Incorrect
We restore original.next to original.next.next and copied.next to copied.next.next to separate the lists.
5fill in blank
hardFill all three blanks to complete the clone function returning the copied list head.
DSA C#
public Node CloneList(Node head) {
if (head == null) return null;
Node current = head;
while (current != null) {
Node copy = new Node([1]);
copy.next = current.[2];
current.next = copy;
current = copy.next;
}
current = head;
while (current != null) {
if (current.random != null) {
current.next.random = current.random.[3];
}
current = current.next.next;
}
Node original = head;
Node copyHead = head.next;
Node copied = copyHead;
while (original != null) {
original.next = original.next.next;
copied.next = copied.next != null ? copied.next.next : null;
original = original.next;
copied = copied.next;
}
return copyHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using random or val incorrectly for next pointers.
Not advancing current correctly in loops.
✗ Incorrect
We create a copy with current.val, link copy.next to current.next, and set copy.random to current.random.next.
