Challenge - 5 Problems
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of adding two linked lists representing numbers?
Given two linked lists representing numbers in reverse order, what is the resulting linked list after adding them?
DSA Typescript
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null { let dummyHead = new ListNode(0); let p = l1, q = l2, current = dummyHead; let carry = 0; while (p !== null || q !== null) { let x = (p !== null) ? p.val : 0; let y = (q !== null) ? q.val : 0; let sum = carry + x + y; carry = Math.floor(sum / 10); current.next = new ListNode(sum % 10); current = current.next; if (p !== null) p = p.next; if (q !== null) q = q.next; } if (carry > 0) { current.next = new ListNode(carry); } return dummyHead.next; } // Input lists: // l1: 2 -> 4 -> 3 -> null (represents 342) // l2: 5 -> 6 -> 4 -> null (represents 465) const l1 = new ListNode(2, new ListNode(4, new ListNode(3))); const l2 = new ListNode(5, new ListNode(6, new ListNode(4))); const result = addTwoNumbers(l1, l2); // Function to print linked list as string function printList(node: ListNode | null): string { let res = []; while (node !== null) { res.push(node.val); node = node.next; } return res.join(' -> ') + ' -> null'; } console.log(printList(result));
Attempts:
2 left
💡 Hint
Add digits one by one from the head, carry over if sum >= 10.
✗ Incorrect
Adding 342 + 465 = 807. The linked list stores digits in reverse order: 7 -> 0 -> 8 -> null.
🧠 Conceptual
intermediateWhy do we use a dummy head node in linked list addition?
In the addTwoNumbers function, why is a dummy head node used before building the result linked list?
Attempts:
2 left
💡 Hint
Think about how to easily add nodes without checking if the list is empty.
✗ Incorrect
A dummy head node helps avoid checking if the result list is empty before adding nodes, simplifying code.
🔧 Debug
advancedWhat error occurs in this code when adding two linked lists?
Identify the error when running this code snippet for adding two numbers represented as linked lists.
DSA Typescript
function addTwoNumbers(l1, l2) {
let dummyHead = new ListNode(0);
let current = dummyHead;
let carry = 0;
while (l1 !== null && l2 !== null) {
let sum = carry + l1.val + l2.val;
carry = Math.floor(sum / 10);
current.next = new ListNode(sum % 10);
current = current.next;
l1 = l1.next;
l2 = l2.next;
}
if (carry > 0) {
current.next = new ListNode(carry);
}
return dummyHead.next;
}Attempts:
2 left
💡 Hint
Check the loop condition and what happens if one list is longer.
✗ Incorrect
Using '&&' in the loop stops when either list ends, missing remaining nodes of the longer list.
❓ Predict Output
advancedWhat is the output of adding linked lists with carry at the end?
What is the output linked list after adding these two numbers represented as linked lists?
DSA Typescript
const l1 = new ListNode(9, new ListNode(9, new ListNode(9))); const l2 = new ListNode(1); const result = addTwoNumbers(l1, l2); function printList(node) { let res = []; while (node !== null) { res.push(node.val); node = node.next; } return res.join(' -> ') + ' -> null'; } console.log(printList(result));
Attempts:
2 left
💡 Hint
Remember to add carry after processing all nodes.
✗ Incorrect
999 + 1 = 1000, so the linked list is 0 -> 0 -> 0 -> 1 -> null.
🚀 Application
expertHow to modify the addition for numbers stored in forward order linked lists?
If the digits are stored in forward order (most significant digit first), which approach correctly adds two numbers represented as linked lists?
Attempts:
2 left
💡 Hint
Think about how addition works starting from least significant digit.
✗ Incorrect
Reversing lists lets us add from least significant digit easily, then reverse result to restore order.
