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* 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning NULL or 0 instead of the passed value.
Assigning the pointer variable instead of the value.
✗ Incorrect
We assign the passed value 'val' to the data field of the new node.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using current->prev which does not exist in singly linked list.
Assigning current to NULL prematurely.
✗ Incorrect
To traverse the linked list, we move to the next node using current->next.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using modulo instead of division for carry.
Adding carry incorrectly.
✗ Incorrect
Carry is the quotient when sum is divided by 10, so carry = sum / 10.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Linking to resultHead instead of newNode.
Using NULL instead of newNode for next pointer.
✗ Incorrect
We create a new node with the sum digit and link it by setting resultTail->next to newNode.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking carry in loop condition.
Returning tail instead of head.
Creating node with carry instead of sum.
✗ Incorrect
Loop continues while carry exists; new node holds sum digit; return head of result list.
