Skip to content

Add Two Numbers

Solve on LeetCodeMedium
Time
Space

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

Two lists 2→4→3 and 5→6→4 adding to 7→0→8

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:

  • The number of nodes in each linked list is in the range [1, 100].
  • 0≤0 \le Node.val ≤9\le 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

A list can have 100 digits, far more than any built-in integer holds, so we must add digit by digit. The lists store the digits in reverse order, so the head of each list is the ones digit — exactly the order in which we add numbers on paper. Walk both lists together, add the two current digits plus the carry, write down sum % 10 as a new node, and carry sum / 10 into the next position.

A dummy head node avoids special-casing the first output node: we always append after the tail p, and the answer is dummy.next.

  1. Create a dummy node dummy, a tail pointer p = &dummy, and carry = 0.
  2. While l1, l2, or carry is non-zero:
    • sum = carry, plus l1->val if l1 is not null (then advance l1), plus l2->val if l2 is not null (then advance l2);
    • append new ListNode(sum % 10) after p, move p to it, and set carry = sum / 10.
  3. Return dummy.next.

For Example 1: 2 + 5 = 7, then 4 + 6 = 10 (write 0, carry 1), then 3 + 4 + 1 = 8, giving 7 → 0 → 8.

  • Time: O(max⁡(m,n))O(\max(m, n)) — each node of each list is visited once, where mm and nn are the list lengths.
  • Space: O(1)O(1) extra — only the output list of max⁡(m,n)+1\max(m, n) + 1 nodes at most is allocated.
  • Different lengths: once one list ends it contributes 0 and the loop continues with the other.
  • Final carry such as Example 3: after both lists end, carry == 1 triggers one more iteration that appends the extra 1.
  • Both zero: one iteration produces the single node 0.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy(0), *p = &dummy;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry;
if (l1) sum += l1->val, l1 = l1->next;
if (l2) sum += l2->val, l2 = l2->next;
p->next = new ListNode(sum % 10);
p = p->next;
carry = sum / 10;
}
return dummy.next;
}
};
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), p = dummy;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int sum = carry;
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
p.next = new ListNode(sum % 10);
p = p.next;
carry = sum / 10;
}
return dummy.next;
}
}
class Solution:
def addTwoNumbers(self, l1: ListNode | None, l2: ListNode | None) -> ListNode | None:
dummy = p = ListNode(0)
carry = 0
while l1 or l2 or carry:
total = carry
if l1:
total += l1.val
l1 = l1.next
if l2:
total += l2.val
l2 = l2.next
p.next = ListNode(total % 10)
p = p.next
carry = total // 10
return dummy.next

Give it a try first

Attempt the problem on LeetCode — the code will still be here.