Challenge - 5 Problems
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Adding Two Linked Lists Representing Numbers
What is the printed linked list after adding these two numbers represented by linked lists?
List 1: 2 -> 4 -> 3 -> null (represents 342)
List 2: 5 -> 6 -> 4 -> null (represents 465)
Code adds the numbers and prints the resulting linked list.
List 1: 2 -> 4 -> 3 -> null (represents 342)
List 2: 5 -> 6 -> 4 -> null (represents 465)
Code adds the numbers and prints the resulting linked list.
DSA C
struct Node {
int val;
struct Node* next;
};
struct Node* addTwoNumbers(struct Node* l1, struct Node* l2) {
struct Node* dummy = malloc(sizeof(struct Node));
dummy->val = 0;
dummy->next = NULL;
struct Node* current = dummy;
int carry = 0;
while (l1 != NULL || l2 != NULL || carry != 0) {
int sum = carry;
if (l1 != NULL) {
sum += l1->val;
l1 = l1->next;
}
if (l2 != NULL) {
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
struct Node* newNode = malloc(sizeof(struct Node));
newNode->val = sum % 10;
newNode->next = NULL;
current->next = newNode;
current = newNode;
}
struct Node* result = dummy->next;
free(dummy);
return result;
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d", head->val);
if (head->next != NULL) printf(" -> ");
head = head->next;
}
printf(" -> null\n");
}
// After calling addTwoNumbers with above lists, printList is called on result.Attempts:
2 left
💡 Hint
Add digits from right to left, carry over if sum >= 10.
✗ Incorrect
The numbers 342 + 465 = 807. The linked list stores digits in reverse order: 7 -> 0 -> 8 -> null.
🧠 Conceptual
intermediate1:30remaining
Understanding Carry in Linked List Addition
Why is it necessary to continue the loop when carry is not zero after processing both linked lists?
Attempts:
2 left
💡 Hint
Think about what happens if the last sum is 15.
✗ Incorrect
If the last addition results in a carry (like 1), it means an extra digit must be added to the result linked list to represent that carry.
🔧 Debug
advanced2:00remaining
Identify the Bug in Linked List Addition Code
What error will this code cause when adding two linked lists?
Code snippet:
while (l1 != NULL || l2 != NULL) { int sum = carry; if (l1 != NULL) { sum += l1->val; l1 = l1->next; } if (l2 != NULL) { sum += l2->val; l2 = l2->next; } carry = sum / 10; struct Node* newNode = malloc(sizeof(struct Node)); newNode->val = sum % 10; newNode->next = NULL; current->next = newNode; current = newNode; } // Missing carry check after loop
Code snippet:
while (l1 != NULL || l2 != NULL) { int sum = carry; if (l1 != NULL) { sum += l1->val; l1 = l1->next; } if (l2 != NULL) { sum += l2->val; l2 = l2->next; } carry = sum / 10; struct Node* newNode = malloc(sizeof(struct Node)); newNode->val = sum % 10; newNode->next = NULL; current->next = newNode; current = newNode; } // Missing carry check after loop
Attempts:
2 left
💡 Hint
What if the last sum is 15?
✗ Incorrect
If carry is not zero after loop ends, it must be added as a new node. Missing this causes wrong result.
❓ Predict Output
advanced2:00remaining
Output of Adding Unequal Length Linked Lists
What is the output linked list after adding these two numbers?
List 1: 9 -> 9 -> 9 -> 9 -> null (represents 9999)
List 2: 1 -> null (represents 1)
Use the standard addition code with carry.
List 1: 9 -> 9 -> 9 -> 9 -> null (represents 9999)
List 2: 1 -> null (represents 1)
Use the standard addition code with carry.
Attempts:
2 left
💡 Hint
Add digit by digit with carry, note length difference.
✗ Incorrect
9999 + 1 = 10000, represented as 0->0->0->0->1->null in reverse order.
🚀 Application
expert1:30remaining
Calculate Length of Result Linked List After Addition
Given two linked lists representing numbers, what is the maximum possible length of the resulting linked list after addition?
Attempts:
2 left
💡 Hint
Consider carry from the last digit addition.
✗ Incorrect
Adding two numbers can produce a carry that adds one extra digit beyond the longer list length.
