Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Add Two Numbers Represented as Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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));
A7 -> 0 -> 7 -> null
B7 -> 0 -> 8 -> null
C7 -> 10 -> 7 -> null
D8 -> 0 -> 7 -> null
Attempts:
2 left
💡 Hint
Add digits node by node, carry over if sum >= 10.
❓ Predict Output
intermediate
2:00remaining
What 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));
A0 -> 0 -> 0 -> 0 -> null
B1 -> 0 -> 0 -> 0 -> 0 -> null
C0 -> 0 -> 0 -> 0 -> 1 -> null
D9 -> 9 -> 9 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember to carry over after the last node if needed.
❓ Predict Output
advanced
2:00remaining
What 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));
A1 -> null
B0 -> null
CError: Cannot read property 'val' of null
Dnull
Attempts:
2 left
💡 Hint
Check if the function handles empty inputs without adding nodes.
❓ Predict Output
advanced
2:00remaining
What 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));
A0 -> 1 -> 1 -> 1 -> null
B0 -> 0 -> 0 -> null
C0 -> 0 -> 1 -> null
D0 -> 1 -> 1 -> null
Attempts:
2 left
💡 Hint
Add digits and carry carefully for each node.
❓ Predict Output
expert
3:00remaining
What 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));
A1001
B1000
C999
D1002
Attempts:
2 left
💡 Hint
Adding two numbers with all 9s causes a carry that adds an extra digit.

Practice

(1/5)
1. What does the problem "Add Two Numbers Represented as Linked List" ask you to do?
easy
A. Add two numbers where each digit is a node in a linked list, and return the sum as a linked list.
B. Multiply two numbers stored in arrays and return the product as an array.
C. Sort two linked lists and merge them into one sorted list.
D. Find the difference between two numbers represented as linked lists.

Solution

  1. Step 1: Understand the problem statement

    The problem requires adding two numbers where each number is represented by a linked list. Each node contains a single digit, and digits are stored in reverse order.
  2. Step 2: Identify the expected output

    The output should be a linked list representing the sum of the two numbers, also in reverse order.
  3. Final Answer:

    Add two numbers represented as linked lists and return their sum as a linked list -> Option A
  4. Quick Check:

    Adding digits node by node = Add linked lists [OK]
Hint: Remember digits are reversed in linked lists [OK]
Common Mistakes:
  • Thinking digits are stored in normal order
  • Confusing addition with multiplication
  • Returning a number instead of a linked list
2. Which of the following is the correct way to create a node for a singly linked list in JavaScript for this problem?
easy
A. function ListNode(val) { this.val = val; this.next = null; }
B. class ListNode { constructor(val) { this.value = val; this.nextNode = null; } }
C. let node = { value: val, next: null };
D. function Node(val) { this.val = val; this.nextNode = null; }

Solution

  1. Step 1: Check the standard node structure

    The node should have a property named 'val' for the digit and 'next' for the next node pointer.
  2. Step 2: Verify the syntax correctness

    function ListNode(val) { this.val = val; this.next = null; } correctly defines a function constructor with 'val' and 'next' properties initialized properly.
  3. Final Answer:

    function ListNode(val) { this.val = val; this.next = null; } -> Option A
  4. Quick Check:

    Node with val and next properties = function ListNode(val) { this.val = val; this.next = null; } [OK]
Hint: Use 'val' and 'next' for node properties [OK]
Common Mistakes:
  • Using 'value' instead of 'val'
  • Using 'nextNode' instead of 'next'
  • Incorrect constructor syntax
3. What is the output linked list after adding the two numbers represented by these linked lists? List 1: 2 -> 4 -> 3 -> null (represents 342) List 2: 5 -> 6 -> 4 -> null (represents 465)
function addTwoNumbers(l1, l2) {
  let dummy = new ListNode(0);
  let current = dummy;
  let carry = 0;
  while (l1 || l2 || carry) {
    let sum = carry;
    if (l1) { sum += l1.val; l1 = l1.next; }
    if (l2) { sum += l2.val; l2 = l2.next; }
    carry = Math.floor(sum / 10);
    current.next = new ListNode(sum % 10);
    current = current.next;
  }
  return dummy.next;
}
medium
A. 7 -> 0 -> 7 -> null
B. 7 -> 0 -> 8 -> null
C. 7 -> 1 -> 7 -> null
D. 8 -> 0 -> 7 -> null

Solution

  1. Step 1: Add digits node by node with carry

    2+5=7 (carry 0), 4+6=10 (carry 1), 3+4+1=8 (carry 0)
  2. Step 2: Form the resulting linked list

    The digits are 7 -> 0 -> 8 representing 807 in reverse order.
  3. Final Answer:

    7 -> 0 -> 8 -> null -> Option B
  4. Quick Check:

    342 + 465 = 807 = 7->0->8 [OK]
Hint: Add digits with carry, build new nodes [OK]
Common Mistakes:
  • Ignoring carry after adding digits
  • Mixing digit order in output
  • Returning sum as a number, not linked list
4. Identify the bug in the following code snippet for adding two numbers represented as linked lists:
function addTwoNumbers(l1, l2) {
  let dummy = new ListNode(0);
  let current = dummy;
  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;
  }
  return dummy.next;
}
medium
A. The function does not return the dummy node itself.
B. The carry is not calculated correctly using Math.floor.
C. The dummy node should be initialized with value 1 instead of 0.
D. The loop condition should use '||' instead of '&&' to handle lists of different lengths.

Solution

  1. Step 1: Analyze the loop condition

    The loop stops when either l1 or l2 becomes null, missing remaining nodes if lists differ in length.
  2. Step 2: Correct the loop condition

    Using '||' ensures the loop continues until both lists and carry are fully processed.
  3. Final Answer:

    The loop condition should use '||' instead of '&&' to handle lists of different lengths. -> Option D
  4. Quick Check:

    Loop condition must cover all nodes = The loop condition should use '||' instead of '&&' to handle lists of different lengths. [OK]
Hint: Use '||' to continue while any list or carry remains [OK]
Common Mistakes:
  • Stopping loop too early with '&&'
  • Ignoring leftover carry after loop
  • Not advancing pointers correctly
5. Given two linked lists representing numbers in reverse order, how would you modify the addition function to handle the case where the input lists have different lengths and the final carry creates a new node? Choose the best approach to ensure correct output for inputs like: List 1: 9 -> 9 -> 9 -> null (represents 999) List 2: 1 -> null (represents 1)
function addTwoNumbers(l1, l2) {
  let dummy = new ListNode(0);
  let current = dummy;
  let carry = 0;
  while (l1 || l2 || carry) {
    let sum = carry;
    if (l1) { sum += l1.val; l1 = l1.next; }
    if (l2) { sum += l2.val; l2 = l2.next; }
    carry = Math.floor(sum / 10);
    current.next = new ListNode(sum % 10);
    current = current.next;
  }
  return dummy.next;
}
hard
A. Add zeros to the shorter list before starting the addition to equalize lengths.
B. Stop the loop when both lists end and ignore carry since it won't affect the sum.
C. Use a loop condition that continues while l1, l2, or carry exists, adding carry as a new node if needed.
D. Only add nodes while both lists have nodes, then append the remaining nodes without addition.

Solution

  1. Step 1: Understand the need to handle different lengths and carry

    When lists differ in length, addition must continue until both lists and carry are fully processed.
  2. Step 2: Use a loop condition that checks l1, l2, or carry

    This ensures the loop adds all digits and creates a new node if carry remains after lists end.
  3. Final Answer:

    Use a loop condition that continues while l1, l2, or carry exists, adding carry as a new node if needed. -> Option C
  4. Quick Check:

    Loop until all digits and carry processed = Use a loop condition that continues while l1, l2, or carry exists, adding carry as a new node if needed. [OK]
Hint: Loop while any list or carry remains to add all digits [OK]
Common Mistakes:
  • Ignoring carry after lists end
  • Trying to pad shorter list manually
  • Appending leftover nodes without adding carry
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard