Bird
0
0
DSA Cprogramming~10 mins

Merge Two Sorted Linked Lists in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ANULL
Bnode
C0
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the input data.
Using the node pointer instead of data.
2fill in blank
medium

Complete 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'
Al1
Bl2
Chead
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the other list.
Returning l1 when it is NULL.
3fill in blank
hard

Fix 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'
A>
B==
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which picks the larger node first.
Using '==' which only checks equality.
4fill in blank
hard

Fill 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'
Al2
Bl1
CNULL
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the other list.
Swapping the return values.
5fill in blank
hard

Fill 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'
Al2
Bl1
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Returning NULL instead of the other list.
Using '>' instead of '<' in comparison.