Bird
0
0
DSA Cprogramming~10 mins

Add Two Numbers Represented as Linked List 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* createNode(int val) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = [1];
    newNode->next = NULL;
    return newNode;
}
Drag options to blanks, or click blank then click option'
AnewNode
B0
CNULL
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the passed value.
Assigning the pointer variable instead of the value.
2fill in blank
medium

Complete the code to move to the next node in the linked list.

DSA C
while (current != NULL) {
    sum += current->data;
    current = [1];
}
Drag options to blanks, or click blank then click option'
Acurrent->next
Bcurrent->prev
Ccurrent
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using current->prev which does not exist in singly linked list.
Assigning current to NULL prematurely.
3fill in blank
hard

Fix the error in the code to correctly add carry to the sum digit.

DSA C
int sum = val1 + val2 + carry;
carry = [1];
sum = sum % 10;
Drag options to blanks, or click blank then click option'
Asum / 10
Bcarry + 1
Csum % 10
Dval1 + val2
Attempts:
3 left
💡 Hint
Common Mistakes
Using modulo instead of division for carry.
Adding carry incorrectly.
4fill in blank
hard

Fill both blanks to correctly create and link the new node in the result list.

DSA C
struct Node* newNode = createNode([1]);
if (resultHead == NULL) {
    resultHead = newNode;
    resultTail = newNode;
} else {
    resultTail->next = [2];
    resultTail = newNode;
}
Drag options to blanks, or click blank then click option'
Asum
BnewNode
CresultHead
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Linking to resultHead instead of newNode.
Using NULL instead of newNode for next pointer.
5fill in blank
hard

Fill all three blanks to correctly handle the loop and carry for adding two linked lists.

DSA C
while (l1 != NULL || l2 != NULL || [1]) {
    int val1 = (l1 != NULL) ? l1->data : 0;
    int val2 = (l2 != NULL) ? l2->data : 0;
    int sum = val1 + val2 + carry;
    carry = sum / 10;
    sum = sum % 10;
    struct Node* newNode = createNode([2]);
    if (resultHead == NULL) {
        resultHead = newNode;
        resultTail = newNode;
    } else {
        resultTail->next = newNode;
        resultTail = newNode;
    }
    if (l1 != NULL) l1 = l1->next;
    if (l2 != NULL) l2 = l2->next;
}
return [3];
Drag options to blanks, or click blank then click option'
Acarry
Bsum
CresultHead
DresultTail
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking carry in loop condition.
Returning tail instead of head.
Creating node with carry instead of sum.