Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA C
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->next = NULL;
newNode->random = NULL; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable instead of the input value.
Assigning the pointer instead of the data value.
✗ Incorrect
The new node's data field should be set to the given value, which is stored in the variable 'value'.
2fill in blank
mediumComplete the code to insert the cloned node right after the original node.
DSA C
clonedNode->next = original->next;
original->next = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning original->next to original instead of clonedNode.
Using an unrelated pointer variable.
✗ Incorrect
To insert the cloned node after the original, set original->next to clonedNode.
3fill in blank
hardFix the error in setting the random pointer of the cloned node.
DSA C
if (original->random != NULL) { original->next->random = original->random[1]2; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->random' instead of '->next' to get the cloned node.
Not checking if original->random is NULL.
✗ Incorrect
The cloned node is right after the original, so to set its random pointer, use original->random->next.
4fill in blank
hardFill both blanks to separate the cloned list from the original list.
DSA C
original->next = original->next[1]; cloned->next = cloned->next[2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->next' only, which does not skip nodes.
Using '->random' which is unrelated here.
✗ Incorrect
To separate lists, skip one node ahead by using '->next->next' for both original and cloned nodes.
5fill in blank
hardFill all three blanks to complete the function that clones the linked list with random pointers.
DSA C
struct Node* cloneList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
struct Node* cloned = (struct Node*)malloc(sizeof(struct Node));
cloned->data = curr->data;
cloned->next = curr->next;
curr->next = [1];
curr = cloned->next;
}
curr = head;
while (curr != NULL) {
if (curr->random != NULL) {
curr->next->random = curr->random[2];
}
curr = curr->next->next;
}
struct Node* original = head;
struct Node* cloneHead = head ? head->next : NULL;
struct Node* cloned = cloneHead;
while (original != NULL && cloned != NULL) {
original->next = original->next[3];
cloned->next = cloned->next[3];
original = original->next;
cloned = cloned->next;
}
return cloneHead;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not inserting cloned node correctly.
Incorrectly setting random pointers.
Not separating lists properly causing cycles.
✗ Incorrect
Insert cloned node after current (curr->next = cloned), set random pointer using original's random's next, and separate lists by skipping nodes with '->next->next'.
