Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Add Two Numbers Represented as Linked List in DSA Typescript - 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 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));
A7 -> 0 -> 8 -> null
B7 -> 0 -> 7 -> null
C7 -> 1 -> 8 -> null
D8 -> 0 -> 7 -> null
Attempts:
2 left
💡 Hint
Add digits one by one from the head, carry over if sum >= 10.
🧠 Conceptual
intermediate
1:30remaining
Why 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?
ATo keep track of the length of the linked lists.
BTo store the carry value during addition.
CTo simplify edge cases by providing a fixed starting point for the result list.
DTo reverse the input linked lists before addition.
Attempts:
2 left
💡 Hint
Think about how to easily add nodes without checking if the list is empty.
🔧 Debug
advanced
2:00remaining
What 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;
}
AThe carry is never reset to zero causing infinite loop.
BThe code throws a TypeError when accessing l1.val.
CThe function returns null instead of the sum list.
DThe loop stops too early if lists have different lengths, missing remaining nodes.
Attempts:
2 left
💡 Hint
Check the loop condition and what happens if one list is longer.
❓ Predict Output
advanced
2:00remaining
What 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));
A0 -> 0 -> 0 -> null
B0 -> 0 -> 0 -> 1 -> null
C1 -> 0 -> 0 -> 0 -> null
D9 -> 9 -> 9 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember to add carry after processing all nodes.
🚀 Application
expert
2:30remaining
How 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?
AReverse both lists, add using existing method, then reverse the result back.
BAdd digits directly from head to tail without reversing, carrying over as usual.
CConvert linked lists to integers, add, then convert back to linked list.
DUse recursion to add digits starting from the tail without reversing.
Attempts:
2 left
💡 Hint
Think about how addition works starting from least significant digit.

Practice

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

Solution

  1. Step 1: Understand the input format

    The numbers are given as linked lists where each node contains a single digit, and digits are stored in reverse order.
  2. Step 2: Understand the required output

    You must add these two numbers digit by digit and return the sum as a linked list in the same reversed format.
  3. Final Answer:

    Add two numbers digit-wise from linked lists and return sum as linked list -> Option B
  4. Quick Check:

    Adding digits node by node = Add two numbers as linked lists [OK]
Hint: Add digits node by node with carry, return linked list [OK]
Common Mistakes:
  • Confusing input format as normal integers
  • Returning sum as integer instead of linked list
  • Ignoring carry over digits
2. Which TypeScript code snippet correctly defines a node for a singly linked list used in this problem?
easy
A. class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val ?? 0; this.next = next ?? null; } }
B. interface ListNode { value: string; next: ListNode; }
C. type ListNode = { val: string; next: ListNode | undefined; };
D. function ListNode(val) { this.val = val; this.next = null; }

Solution

  1. Step 1: Check for correct TypeScript class syntax

    class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val ?? 0; this.next = next ?? null; } } defines a class with val as number and next as ListNode or null, with a constructor and default values.
  2. Step 2: Verify other options

    interface ListNode { value: string; next: ListNode; } uses interface with string type and no null for next, type ListNode = { val: string; next: ListNode | undefined; }; uses string val and next possibly undefined, function ListNode(val) { this.val = val; this.next = null; } is JavaScript function style without types.
  3. Final Answer:

    TypeScript class with number val and nullable next -> Option A
  4. Quick Check:

    TypeScript class with number val and nullable next = class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val ?? 0; this.next = next ?? null; } } [OK]
Hint: Use class with val:number and next: ListNode|null [OK]
Common Mistakes:
  • Using string instead of number for val
  • Not allowing next to be null
  • Using JavaScript function instead of TypeScript class
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) Use the standard addition method for linked lists.
medium
A. 7 -> 10 -> 7 -> null
B. 8 -> 0 -> 7 -> null
C. 7 -> 0 -> 8 -> null
D. 2 -> 0 -> 8 -> null

Solution

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

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

    Digits are 7, 0 (from 10), and 8, so linked list is 7 -> 0 -> 8 -> null
  3. Final Answer:

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

    342 + 465 = 807 represented as 7->0->8 [OK]
Hint: Add digits with carry, store remainder in node [OK]
Common Mistakes:
  • Not carrying over when sum >= 10
  • Reversing the order of digits in output
  • Adding digits incorrectly without carry
4. Given this TypeScript function to add two numbers as linked lists, what is the bug?
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
  let dummy = new ListNode(0);
  let current = dummy;
  let carry = 0;
  while (l1 !== null && l2 !== null) {
    let sum = l1.val + l2.val + carry;
    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. Carry is not calculated correctly using Math.floor.
B. The function returns dummy instead of dummy.next.
C. Dummy node is not initialized properly.
D. The loop stops when either list ends, missing remaining nodes in longer list.

Solution

  1. Step 1: Analyze the while loop condition

    The loop runs only while both l1 and l2 are not null, so it stops if one list is longer.
  2. Step 2: Identify missing handling of remaining nodes

    After the loop, remaining nodes of the longer list and any carry are not processed, causing incorrect result.
  3. Final Answer:

    Loop stops too early, missing remaining nodes in longer list -> Option D
  4. Quick Check:

    Loop condition && misses longer list nodes [OK]
Hint: Use || in loop to handle different length lists [OK]
Common Mistakes:
  • Using && instead of || in loop condition
  • Not processing leftover carry after loop
  • Returning dummy instead of dummy.next
5. You have two linked lists representing numbers: List 1: 9 -> 9 -> 9 -> 9 -> null (9999) and List 2: 1 -> null (1). What is the correct resulting linked list after adding them?
hard
A. 0 -> 0 -> 0 -> 0 -> 1 -> null
B. 1 -> 0 -> 0 -> 0 -> 0 -> null
C. 0 -> 0 -> 0 -> 0 -> null
D. 9 -> 9 -> 9 -> 10 -> null

Solution

  1. Step 1: Add digits with carry from right to left

    9+1=10 (0 carry 1), next 9+0+1=10 (0 carry 1), repeat for next two 9s with carry 1.
  2. Step 2: Add final carry as new node

    After last 9+0+1=10, carry 1 remains, so add new node with 1 at the end.
  3. Final Answer:

    0 -> 0 -> 0 -> 0 -> 1 -> null -> Option A
  4. Quick Check:

    9999 + 1 = 10000 as linked list 0->0->0->0->1 [OK]
Hint: Add carry after last node if needed [OK]
Common Mistakes:
  • Not adding extra node for final carry
  • Stopping addition early when one list ends
  • Incorrectly placing carry node position
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