Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the sum digit.
DSA C#
ListNode newNode = new ListNode([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using sum / 10 instead of sum % 10.
Adding 10 to sum instead of taking modulo.
Multiplying sum by 10.
✗ Incorrect
We use sum % 10 to get the digit to store in the current node because each node stores a single digit.
2fill in blank
mediumComplete the code to update the carry for the next addition.
DSA C#
carry = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using modulo (%) instead of division for carry.
Adding 10 to sum instead of dividing.
Multiplying sum by 10.
✗ Incorrect
Carry is the digit to carry over to the next node, which is sum divided by 10 (integer division).
3fill in blank
hardFix the error in the loop condition to continue while either list has nodes or carry is non-zero.
DSA C#
while (l1 != null || l2 != null || [1] != 0) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum instead of carry in the condition.
Using node variables instead of carry.
Not including carry in the loop condition.
✗ Incorrect
We continue the loop if carry is not zero to add the leftover carry digit.
4fill in blank
hardFill both blanks to correctly move to the next nodes in the lists.
DSA C#
l1 = l1 != null ? [1] : null; l2 = l2 != null ? [2] : null;
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning l1 or l2 directly without .next.
Swapping l1.next and l2.next.
Not checking for null before moving.
✗ Incorrect
We move l1 and l2 to their next nodes if they are not null.
5fill in blank
hardFill all three blanks to build the linked list and return the result.
DSA C#
ListNode dummyHead = new ListNode(0); ListNode current = dummyHead; while (l1 != null || l2 != null || carry != 0) { int x = (l1 != null) ? l1.val : 0; int y = (l2 != null) ? l2.val : 0; int sum = x + y + carry; carry = sum / 10; current.next = new ListNode([1]); current = [2]; l1 = (l1 != null) ? l1.next : null; l2 = (l2 != null) ? l2.next : null; } return [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dummyHead instead of dummyHead.next.
Assigning current incorrectly.
Using sum instead of sum % 10 for node value.
✗ Incorrect
We create a new node with sum % 10, move current to current.next, and return dummyHead.next as the head of the result list.
