Home
DSA Patterns
Linked List
Add Two Numbers
Add Two Numbers
Add two numbers stored as reversed linked lists of digits, column by column with a carry, building the result through a dummy head.
Problem Understanding
Add Two Numbers: two non-negative integers are stored as linked lists, one digit per node, least significant digit first. Return their sum as a list in the same form.
Example: l1 = [2, 4, 3] is 342 and l2 = [5, 6, 4] is 465; 342 + 465 = 807, so return [7, 0, 8].
Attempt 1: Convert to Integers
Read both lists into numbers, add them and write the sum back out. It breaks as soon as the numbers are longer than a 64-bit integer holds, and the lists can have up to 100 digits. The sum has to be built digit by digit, the way you add on paper.
The Intuition: Column Addition, Head First
On paper you add the rightmost column first and carry left. Because these lists store the ones digit at the head, walking both lists from the start is walking those columns in exactly that order.
total = carry + l1 digit + l2 digit, using0for a list that has run out.- Append a node with
total % 10; the new carry istotal / 10. - Keep going while either list or the carry remains.
A dummy head gives the result list a node to hang the first digit on, so there is no special case for an empty result.
Interactive Walkthrough
DIGITS ARE STORED IN REVERSE: THE HEAD IS THE ONES DIGIT
DUMMY HEAD
l1
0
1
2
3
l2
0
1
2
3
result
0
1
2
3
total
—
carry
0
sum
—
dummy → (empty result), carry = 0
Next
Column 0: 0 (carry) + 2 + 5 = 7. Digit 7, carry 0
l1
l2
Legend & Complexity
The column being added
Result digits written so far
Time
O(max(m, n))
Space
O(max(m, n)) for the result
l1, l2 and the result are drawn head first, so each column is a vertical slice through all three. Each round adds one column with the carry and appends one digit; with a final carry, the result grows one node past both inputs.
Dry Run Table
Input: l1 = [2, 4, 3], l2 = [5, 6, 4]
| Column | carry in | l1 | l2 | total | digit | carry out |
|---|---|---|---|---|---|---|
| 0 | 0 | 2 | 5 | 7 | 7 | 0 |
| 1 | 0 | 4 | 6 | 10 | 0 | 1 |
| 2 | 1 | 3 | 4 | 8 | 8 | 0 |
Result: [7, 0, 8], which is 807.
The Solution Template
Column addition through a dummy head, looping while either list or the carry remains.
Add Two Numbers Code
1var addTwoNumbers = function(l1, l2) {2 const dummy = new ListNode();3 let tail = dummy, carry = 0;4 while (l1 || l2 || carry) {5 const total = carry + (l1 ? l1.val : 0) + (l2 ? l2.val : 0);6 carry = Math.floor(total / 10);7 tail.next = new ListNode(total % 10);8 tail = tail.next;9 l1 = l1 ? l1.next : null;10 l2 = l2 ? l2.next : null;11 }12 return dummy.next;13};
Edge Cases & Common Mistakes
- A final carry:
[9, 9] + [1]is[0, 0, 1]; leavingcarryout of the loop condition loses the last digit. - Different lengths: treat a list that has ended as contributing
0. - Converting to integers: overflows for long inputs; add digit by digit instead.
- Forgetting the dummy head: the first digit then needs its own special case.
Summary
Reversed digits make the list walk the same as column addition from the right. Add the two digits and the carry, append the ones digit through a dummy head, and loop while anything is left, including the carry.
More practice: DSA interview questions with answers · patterns cheat sheet
