📖 Scenario: Imagine you have two numbers, but each digit is stored in a linked list node. The digits are stored in reverse order, so the 1's digit is at the head of the list. Your task is to add these two numbers and return the sum as a linked list in the same reversed order.
🎯 Goal: You will build a program that adds two numbers represented by linked lists and outputs the resulting linked list showing the sum.
📋 What You'll Learn
Create two linked lists representing the numbers 342 and 465
Create a variable to hold the carry during addition
Write the logic to add the two linked lists digit by digit
Print the resulting linked list showing the sum
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications where dynamic data storage is needed. Adding numbers as linked lists is a common problem in coding interviews and helps understand how to manipulate linked data.
💼 Career
Understanding linked lists and how to perform operations like addition on them is important for software engineers, especially in roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the two linked lists
Create two linked lists called l1 and l2 representing the numbers 342 and 465 respectively. Each node should have a val property for the digit and a next property pointing to the next node or null. The digits are stored in reverse order, so l1 should be 2 -> 4 -> 3 -> null and l2 should be 5 -> 6 -> 4 -> null.
DSA Javascript
Hint
Use the ListNode constructor to create nodes. Remember the digits are reversed.
2
Create a carry variable
Create a variable called carry and set it to 0. This will hold the carry value during addition.
DSA Javascript
Hint
Use let carry = 0; to create the carry variable.
3
Add the two numbers represented by the linked lists
Write the logic to add the two linked lists l1 and l2 digit by digit. Create a new linked list called result to store the sum. Use variables p1 and p2 to traverse l1 and l2. Use a dummy head node called dummy to simplify list construction. Update carry as needed.
DSA Javascript
Hint
Use a while loop to traverse both lists and add digits. Use carry to handle sums over 9.
4
Print the resulting linked list
Write code to print the resulting linked list result in the format: 7 -> 0 -> 8 -> null. Use a variable node to traverse result and build a string showing each digit followed by ->. End with null.
DSA Javascript
Hint
Traverse the result linked list and build a string showing each digit followed by ->. End with null.
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
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.
Step 2: Identify the expected output
The output should be a linked list representing the sum of the two numbers, also in reverse order.
Final Answer:
Add two numbers represented as linked lists and return their sum as a linked list -> Option A
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
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.
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.
Final Answer:
function ListNode(val) { this.val = val; this.next = null; } -> Option A
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;
}
The digits are 7 -> 0 -> 8 representing 807 in reverse order.
Final Answer:
7 -> 0 -> 8 -> null -> Option B
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
Step 1: Analyze the loop condition
The loop stops when either l1 or l2 becomes null, missing remaining nodes if lists differ in length.
Step 2: Correct the loop condition
Using '||' ensures the loop continues until both lists and carry are fully processed.
Final Answer:
The loop condition should use '||' instead of '&&' to handle lists of different lengths. -> Option D
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
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.
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.
Final Answer:
Use a loop condition that continues while l1, l2, or carry exists, adding carry as a new node if needed. -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.