Browse curriculum

Add Two Numbers (LeetCode 2)

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.

Add Two Numbers Visualization

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.

Add Two Numbers Dry Run

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.

Add Two Numbers LeetCode Solution

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};

Add Two Numbers in Python, Java, C++, TypeScript and C

Add Two Numbers in Python
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = tail = ListNode()
        carry = 0
        while l1 or l2 or carry:
            total = carry + (l1.val if l1 else 0) + (l2.val if l2 else 0)
            carry, digit = divmod(total, 10)
            tail.next = ListNode(digit)
            tail = tail.next
            l1 = l1.next if l1 else None
            l2 = l2.next if l2 else None
        return dummy.next
Add Two Numbers in Java
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(), tail = dummy;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            int total = carry + (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0);
            carry = total / 10;
            tail.next = new ListNode(total % 10);
            tail = tail.next;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        return dummy.next;
    }
}
Add Two Numbers in C++
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode dummy, *tail = &dummy;
        int carry = 0;
        while (l1 || l2 || carry) {
            int total = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
            carry = total / 10;
            tail->next = new ListNode(total % 10);
            tail = tail->next;
            if (l1) l1 = l1->next;
            if (l2) l2 = l2->next;
        }
        return dummy.next;
    }
};
Add Two Numbers in TypeScript
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
    const dummy = new ListNode();
    let tail = dummy, carry = 0;
    while (l1 || l2 || carry) {
        const total = carry + (l1 ? l1.val : 0) + (l2 ? l2.val : 0);
        carry = Math.floor(total / 10);
        tail.next = new ListNode(total % 10);
        tail = tail.next;
        l1 = l1 ? l1.next : null;
        l2 = l2 ? l2.next : null;
    }
    return dummy.next;
}
Add Two Numbers in C
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode dummy = {0, NULL}, *tail = &dummy;
    int carry = 0;
    while (l1 || l2 || carry) {
        int total = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
        carry = total / 10;
        tail->next = calloc(1, sizeof(struct ListNode));
        tail->next->val = total % 10;
        tail = tail->next;
        if (l1) l1 = l1->next;
        if (l2) l2 = l2->next;
    }
    return dummy.next;
}

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.

New to the technique behind this problem? Watch it run step by step:

Singly Linked List Visualization

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

AI resume checker: match your resume to the job description