Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given value.
DSA C
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = [1];
node->next = NULL;
return node;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the input data.
Using the node pointer instead of data.
✗ Incorrect
The new node's data field should be set to the input parameter 'data'.
2fill in blank
mediumComplete the code to return the merged list head when one list is empty.
DSA C
struct Node* mergeSortedLists(struct Node* l1, struct Node* l2) {
if (l1 == NULL) return [1];
if (l2 == NULL) return l1;
// merging logic continues...
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the other list.
Returning l1 when it is NULL.
✗ Incorrect
If the first list is empty, return the second list as the merged result.
3fill in blank
hardFix the error in the comparison to choose the smaller node.
DSA C
if (l1->data [1] l2->data) { head = l1; head->next = mergeSortedLists(l1->next, l2); } else { head = l2; head->next = mergeSortedLists(l1, l2->next); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which picks the larger node first.
Using '==' which only checks equality.
✗ Incorrect
We pick the smaller node, so use '<' to compare data values.
4fill in blank
hardFill both blanks to complete the recursive merge function.
DSA C
struct Node* mergeSortedLists(struct Node* l1, struct Node* l2) {
if (l1 == NULL) return [1];
if (l2 == NULL) return [2];
if (l1->data < l2->data) {
l1->next = mergeSortedLists(l1->next, l2);
return l1;
} else {
l2->next = mergeSortedLists(l1, l2->next);
return l2;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the other list.
Swapping the return values.
✗ Incorrect
Return the other list when one is NULL: if l1 is NULL return l2, if l2 is NULL return l1.
5fill in blank
hardFill all three blanks to create a function that merges two sorted linked lists.
DSA C
struct Node* mergeSortedLists(struct Node* l1, struct Node* l2) {
if (l1 == NULL) return [1];
if (l2 == NULL) return [2];
if (l1->data [3] l2->data) {
l1->next = mergeSortedLists(l1->next, l2);
return l1;
} else {
l2->next = mergeSortedLists(l1, l2->next);
return l2;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the other list.
Using '>' instead of '<' in comparison.
✗ Incorrect
Return the other list if one is NULL, and use '<' to pick the smaller node.
