DSA AnimatorDSA animations
Add Two Numbers LC #2 Medium Linked List · Math · Carry
Problem

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

Example 1
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807
Example 2
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Explanation: 9999999 + 9999 = 10009998
Example 3
Input: l1 = [0], l2 = [0]
Output: [0]
Constraints: 1 ≤ length ≤ 100  |  0 ≤ Node.val ≤ 9  |  No leading zeros (except "0" itself)
Try Examples
Addition Visualization
l1 (digits in reverse — least significant first)
l2 (digits in reverse — least significant first)
Result (built left to right)
d1 — + d2 — + carry —
sum — = digit — (sum%10)
new carry — (sum/10)
CARRY
0
propagates to next column
l1 = —
l2 = —
sum = —
State Variables
d1 (l1.val)
—
d2 (l2.val)
—
carry (in)
0
sum
—
new digit
—
Step Logic
Press ▶ Play or Next Step to begin the animation.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Init dummy node, curr = dummy, carry = 0
2
Loop while l1 != null || l2 != null || carry != 0
3
Read d1 = l1.val (or 0), d2 = l2.val (or 0)
4
Compute sum = d1 + d2 + carry; new carry = sum / 10
5
Append node sum % 10 to result; advance pointers
6
Return dummy.next
Time
O(max(m,n))
Space
O(max(m,n))
Key Insight — Why Reverse Order Helps

Storing digits in reverse order means the least-significant digit comes first. This lets us add digit-by-digit from left to right — exactly how long addition works — without needing to reverse anything. A carry from one column flows naturally to the next node. If the final carry is 1 (e.g. 999 + 1), we append one more node without any special handling.

How to solve Add Two Numbers

LeetCode 2 · Medium · Linked List · Pattern: Linked List Arithmetic

Why the obvious approach is slow

Converting the lists to integers can overflow for long lists. Instead, add digit by digit, like on paper.

The key idea

The lowest digit comes first, which is exactly where addition starts. Add the two digits plus the carry, write the sum mod 10, and carry the rest. Keep going while either list or the carry remains.

Remember

Reversed digits mean you can add from the head. Don't forget a final carry node.

Run the animation above on your own input, or watch the 1:09 video explanation of Add Two Numbers with a dry run and Java code.

More Linked List problems