Browse curriculum

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.

  1. total = carry + l1 digit + l2 digit, using 0 for a list that has run out.
  2. Append a node with total % 10; the new carry is total / 10.
  3. 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

2

0

4

1

3

2

3

l2

5

0

6

1

4

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]; leaving carry out 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

The approach, step by step

  1. Start a dummy head

    Create a dummy node, point tail at it, and set carry = 0.

  2. Add a column

    While either list or the carry remains, total = carry + digit of l1 (or 0) + digit of l2 (or 0); carry = total / 10.

  3. Append and advance

    Append a node with total % 10, move tail to it, and advance whichever lists remain. Return dummy.next.

Frequently asked questions

Why are the digits stored in reverse order?

Addition starts at the ones column. With the least significant digit at the head, walking both lists from the start is exactly walking the columns from right to left, so the carry always flows in the direction the walk goes.


Why is carry part of the loop condition?

When both lists end with a carry left over, as in 99 + 1, one more digit has to be written. Looping while l1, l2 or carry remains writes that final 1 without a special case after the loop.


What changes if the digits are stored most significant first?

That is Add Two Numbers II. Push both lists' digits onto stacks (or reverse the lists), add from the top, and build the result by inserting each new digit at the front.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description