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 where each node contains a single digit and the digits are stored in reverse order, what is the resulting linked list after adding them?
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function addTwoNumbers(l1, l2) {
let dummyHead = new ListNode(0);
let current = dummyHead;
let carry = 0;
while (l1 !== null || l2 !== null || carry !== 0) {
let sum = carry;
if (l1 !== null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 !== null) {
sum += l2.val;
l2 = l2.next;
}
carry = Math.floor(sum / 10);
current.next = new ListNode(sum % 10);
current = current.next;
}
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) {
let output = '';
while (node !== null) {
output += node.val + ' -> ';
node = node.next;
}
output += 'null';
return output;
}
console.log(printList(result));Attempts:
2 left
💡 Hint
Add digits node by node, carry over if sum >= 10.
✗ Incorrect
Adding 342 + 465 = 807. The linked list stores digits in reverse order: 7 -> 0 -> 8 -> null.
❓ Predict Output
intermediateWhat is the output when adding lists of different lengths?
Add two linked lists representing numbers where one list is longer than the other.
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function addTwoNumbers(l1, l2) {
let dummyHead = new ListNode(0);
let current = dummyHead;
let carry = 0;
while (l1 !== null || l2 !== null || carry !== 0) {
let sum = carry;
if (l1 !== null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 !== null) {
sum += l2.val;
l2 = l2.next;
}
carry = Math.floor(sum / 10);
current.next = new ListNode(sum % 10);
current = current.next;
}
return dummyHead.next;
}
// Input lists:
// l1: 9 -> 9 -> 9 -> 9 -> null (represents 9999)
// l2: 1 -> null (represents 1)
const l1 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
const l2 = new ListNode(1);
const result = addTwoNumbers(l1, l2);
function printList(node) {
let output = '';
while (node !== null) {
output += node.val + ' -> ';
node = node.next;
}
output += 'null';
return output;
}
console.log(printList(result));Attempts:
2 left
💡 Hint
Remember to carry over after the last node if needed.
✗ Incorrect
9999 + 1 = 10000, represented as 0 -> 0 -> 0 -> 0 -> 1 -> null in reverse order.
❓ Predict Output
advancedWhat is the output when adding two empty linked lists?
What does the function return when both input linked lists are null?
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function addTwoNumbers(l1, l2) {
let dummyHead = new ListNode(0);
let current = dummyHead;
let carry = 0;
while (l1 !== null || l2 !== null || carry !== 0) {
let sum = carry;
if (l1 !== null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 !== null) {
sum += l2.val;
l2 = l2.next;
}
carry = Math.floor(sum / 10);
current.next = new ListNode(sum % 10);
current = current.next;
}
return dummyHead.next;
}
const l1 = null;
const l2 = null;
const result = addTwoNumbers(l1, l2);
function printList(node) {
if (node === null) return 'null';
let output = '';
while (node !== null) {
output += node.val + ' -> ';
node = node.next;
}
output += 'null';
return output;
}
console.log(printList(result));Attempts:
2 left
💡 Hint
Check if the function handles empty inputs without adding nodes.
✗ Incorrect
Both lists are empty and no carry exists, so the result is null (empty list).
❓ Predict Output
advancedWhat is the output when adding lists with multiple carry overs?
Add two linked lists where each addition causes a carry to the next digit.
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function addTwoNumbers(l1, l2) {
let dummyHead = new ListNode(0);
let current = dummyHead;
let carry = 0;
while (l1 !== null || l2 !== null || carry !== 0) {
let sum = carry;
if (l1 !== null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 !== null) {
sum += l2.val;
l2 = l2.next;
}
carry = Math.floor(sum / 10);
current.next = new ListNode(sum % 10);
current = current.next;
}
return dummyHead.next;
}
// Input lists:
// l1: 5 -> 6 -> 7 -> null (represents 765)
// l2: 5 -> 4 -> 3 -> null (represents 345)
const l1 = new ListNode(5, new ListNode(6, new ListNode(7)));
const l2 = new ListNode(5, new ListNode(4, new ListNode(3)));
const result = addTwoNumbers(l1, l2);
function printList(node) {
let output = '';
while (node !== null) {
output += node.val + ' -> ';
node = node.next;
}
output += 'null';
return output;
}
console.log(printList(result));Attempts:
2 left
💡 Hint
Add digits and carry carefully for each node.
✗ Incorrect
765 + 345 = 1110, reversed as 0 -> 1 -> 1 -> 1 -> null.
❓ Predict Output
expertWhat is the output when adding very large numbers represented as linked lists?
Add two very large numbers represented as linked lists with 1000 nodes each, all digits 9. What is the length of the resulting linked list?
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function addTwoNumbers(l1, l2) {
let dummyHead = new ListNode(0);
let current = dummyHead;
let carry = 0;
while (l1 !== null || l2 !== null || carry !== 0) {
let sum = carry;
if (l1 !== null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 !== null) {
sum += l2.val;
l2 = l2.next;
}
carry = Math.floor(sum / 10);
current.next = new ListNode(sum % 10);
current = current.next;
}
return dummyHead.next;
}
// Create two linked lists of 1000 nodes each, all 9s
function createList(length) {
let head = new ListNode(9);
let current = head;
for (let i = 1; i < length; i++) {
current.next = new ListNode(9);
current = current.next;
}
return head;
}
const l1 = createList(1000);
const l2 = createList(1000);
const result = addTwoNumbers(l1, l2);
// Count length of result
function countLength(node) {
let count = 0;
while (node !== null) {
count++;
node = node.next;
}
return count;
}
console.log(countLength(result));Attempts:
2 left
💡 Hint
Adding two numbers with all 9s causes a carry that adds an extra digit.
✗ Incorrect
Adding two 1000-digit numbers of all 9s results in a 1001-digit number: 1 followed by 1000 zeros.
