Problem
Section titled “Problem”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:

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]. -
Node.val - It is guaranteed that the list represents a number that does not have leading zeros.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”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.
Algorithm
Section titled “Algorithm”- Create a dummy node
dummy, a tail pointerp = &dummy, andcarry = 0. - While
l1,l2, orcarryis non-zero:sum = carry, plusl1->valifl1is not null (then advancel1), plusl2->valifl2is not null (then advancel2);- append
new ListNode(sum % 10)afterp, movepto it, and setcarry = sum / 10.
- 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.
Complexity
Section titled “Complexity”- Time: — each node of each list is visited once, where and are the list lengths.
- Space: extra — only the output list of nodes at most is allocated.
Edge cases
Section titled “Edge cases”- Different lengths: once one list ends it contributes
0and the loop continues with the other. - Final carry such as Example 3: after both lists end,
carry == 1triggers one more iteration that appends the extra1. - 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.nextGive it a try first
Attempt the problem on LeetCode — the code will still be here.