Challenge - 5 Problems
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Adding Two Numbers in Linked List Form
What is the output linked list after adding these two numbers represented as linked lists?
List 1: 2 -> 4 -> 3 -> null (represents 342)
List 2: 5 -> 6 -> 4 -> null (represents 465)
Use the standard addition where digits are stored in reverse order.
List 1: 2 -> 4 -> 3 -> null (represents 342)
List 2: 5 -> 6 -> 4 -> null (represents 465)
Use the standard addition where digits are stored in reverse order.
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}
return dummy->next;
}Attempts:
2 left
💡 Hint
Add digits from the head, carry over if sum >= 10.
✗ Incorrect
Adding 342 + 465 = 807. Stored in reverse order: 7 -> 0 -> 8 -> null.
❓ Predict Output
intermediateResult of Adding Unequal Length Linked Lists
What is the output linked list after adding these two numbers represented as linked lists?
List 1: 9 -> 9 -> 9 -> 9 -> null (represents 9999)
List 2: 1 -> null (represents 1)
Digits are stored in reverse order.
List 1: 9 -> 9 -> 9 -> 9 -> null (represents 9999)
List 2: 1 -> null (represents 1)
Digits are stored in reverse order.
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}
return dummy->next;
}Attempts:
2 left
💡 Hint
Remember to carry over after last digit.
✗ Incorrect
9999 + 1 = 10000, stored as 0 -> 0 -> 0 -> 0 -> 1 -> null.
🔧 Debug
advancedIdentify the Bug in Linked List Addition Code
What error will this code produce when adding two linked lists?
Code snippet:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(0); ListNode* current = dummy; int carry = 0; while (l1 != nullptr && l2 != nullptr) { int sum = l1->val + l2->val + carry; carry = sum / 10; current->next = new ListNode(sum % 10); current = current->next; l1 = l1->next; l2 = l2->next; } return dummy->next; }
Code snippet:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(0); ListNode* current = dummy; int carry = 0; while (l1 != nullptr && l2 != nullptr) { int sum = l1->val + l2->val + carry; carry = sum / 10; current->next = new ListNode(sum % 10); current = current->next; l1 = l1->next; l2 = l2->next; } return dummy->next; }
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;
int carry = 0;
while (l1 != nullptr && l2 != nullptr) {
int sum = l1->val + l2->val + carry;
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
l1 = l1->next;
l2 = l2->next;
}
return dummy->next;
}Attempts:
2 left
💡 Hint
Check what happens after the while loop ends.
✗ Incorrect
The loop stops when either list ends, so leftover digits and carry are ignored.
❓ Predict Output
advancedOutput of Adding Large Numbers with Carry
What is the output linked list after adding these two numbers?
List 1: 9 -> 8 -> 7 -> 6 -> 5 -> null (represents 56789)
List 2: 1 -> 2 -> 3 -> 4 -> 5 -> null (represents 54321)
Digits stored in reverse order.
List 1: 9 -> 8 -> 7 -> 6 -> 5 -> null (represents 56789)
List 2: 1 -> 2 -> 3 -> 4 -> 5 -> null (represents 54321)
Digits stored in reverse order.
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}
return dummy->next;
}Attempts:
2 left
💡 Hint
Add digit by digit with carry, watch for final carry digit.
✗ Incorrect
56789 + 54321 = 111110. Stored reversed: 0 -> 1 -> 1 -> 1 -> 1 -> 1 -> null.
🧠 Conceptual
expertWhy Use a Dummy Node in Linked List Addition?
Why do we use a dummy node at the start of the linked list when adding two numbers represented as linked lists?
Attempts:
2 left
💡 Hint
Think about how linked lists are built node by node.
✗ Incorrect
Dummy node acts as a fixed starting point, so we don't need to check if head is null when adding nodes.
