const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Add Two Numbers LeetCode Solution: Linked List Carry Explained

LeetCode problem #2 is one of the first Medium problems most people encounter — it teaches linked list traversal with carry propagation, the core pattern behind all digit-by-digit math problems.

7 min read|

Add Two Numbers (#2): digit-by-digit carry on linked lists

LeetCode problem #2 — linked list traversal with carry propagation

Add Two Numbers: LeetCode's Second Problem

Add Two Numbers (#2) is the second problem on LeetCode and one of the most solved Medium problems on the platform. If you just finished Two Sum and are looking for your next challenge, this is almost certainly where you end up. It introduces linked lists, carry propagation, and the dummy node technique — three concepts that appear in dozens of other problems.

The add two numbers leetcode problem is deceptively simple on the surface. You are given two numbers represented as reversed linked lists, and you need to return their sum as a new linked list. The twist is that the digits are stored in reverse order, which actually makes the addition easier — you process from least significant to most significant, exactly like you would add numbers by hand.

This problem is a gateway to understanding how linked list traversal works with state (the carry). Once you internalize the pattern here, problems like Add Binary (#67), Plus One (#66), and Multiply Strings (#43) become much more approachable.

Understanding the 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. Your task is to add the two numbers and return the sum as a linked list, also in reverse order.

For example, the number 342 is stored as 2 -> 4 -> 3, and the number 465 is stored as 5 -> 6 -> 4. Adding 342 + 465 = 807, so the result is 7 -> 0 -> 8. The reversed storage is actually a gift — it means you can traverse both lists from head to tail and add digits in the correct order, from ones place to tens place to hundreds place.

The key constraint to watch for is the carry. When two digits sum to 10 or more, you carry the 1 to the next position, exactly like elementary school addition. If the carry persists after both lists are exhausted, you need an extra node at the end.

  • Input: Two linked lists representing reversed integers (e.g., [2,4,3] = 342)
  • Output: A new linked list representing their reversed sum
  • Each node holds a single digit (0-9)
  • The lists may have different lengths
  • A final carry may produce an extra digit (e.g., 99 + 1 = 100)
ℹ️

Did You Know?

Add Two Numbers (#2) is LeetCode's second problem and one of the most solved Mediums — it's often the first linked list problem candidates encounter after Two Sum.

The Approach: Digit by Digit Addition

The approach for add two numbers on leetcode mirrors how you would add numbers by hand. Start at the least significant digit (the head of each list), add the two digits plus any carry from the previous step, write down the ones digit, and carry the tens digit forward. Repeat until both lists are exhausted and the carry is zero.

You traverse both lists simultaneously using two pointers. At each step, you grab the current digit from each list (or 0 if that list is already exhausted), compute the sum including the carry, and create a new node with the result modulo 10. The carry for the next step is the sum divided by 10 (integer division).

The critical insight is the loop condition: continue while l1 is not null OR l2 is not null OR carry is non-zero. That final "OR carry" condition is what handles the edge case where the sum produces an extra digit — for example, 999 + 1 = 1000, which needs four nodes even though the longer input only has three.

Implementation with Dummy Node

The cleanest way to build the result list is with a dummy node. Create a dummy head node that serves as a placeholder — it simplifies the logic because you never have to special-case the first node. You track a current pointer that starts at the dummy and advances as you append new nodes.

Here is the core logic: initialize carry to 0 and current to the dummy node. While l1 or l2 or carry is truthy, compute x = l1.val (or 0), y = l2.val (or 0), and sum = x + y + carry. Create a new node with value sum % 10, set current.next to that node, advance current. Update carry = Math.floor(sum / 10). Advance l1 and l2 if they are not null.

When the loop finishes, dummy.next points to the head of your result list. The time complexity is O(max(m, n)) where m and n are the lengths of the two lists. Space complexity is also O(max(m, n)) for the new list, plus O(1) extra space for the pointers and carry.

  1. 1Create a dummy head node and set current = dummy
  2. 2Initialize carry = 0
  3. 3While l1 or l2 or carry: get digits (or 0 if null), compute sum + carry
  4. 4Create new node with sum % 10, link it to current.next, advance current
  5. 5Update carry = Math.floor(sum / 10), advance l1 and l2 if not null
  6. 6Return dummy.next as the result head
💡

Pro Tip

Use a dummy node and track carry separately — the loop runs while l1 OR l2 OR carry is non-zero. The 'OR carry' part handles the final carry (e.g., 99+1=100) without extra code.

Visual Walkthrough: 342 + 465 = 807

Let us walk through the classic example step by step. We have l1 = [2, 4, 3] representing 342 and l2 = [5, 6, 4] representing 465. Our carry starts at 0.

Step 1: x=2, y=5, sum=2+5+0=7. New node: 7. Carry: 0. Result so far: [7]. Step 2: x=4, y=6, sum=4+6+0=10. New node: 0 (10%10). Carry: 1 (10/10). Result so far: [7, 0]. Step 3: x=3, y=4, sum=3+4+1=8. New node: 8. Carry: 0. Result so far: [7, 0, 8].

Both lists are exhausted and carry is 0, so the loop ends. The result [7, 0, 8] represents 807, which is correct. Each step takes constant time — we simply add two digits and a carry, create one node, and advance the pointers.

Edge Cases to Watch For

The most common edge case that trips people up in the carry addition linked list problem is the final carry. Consider 99 + 1: l1 = [9, 9] and l2 = [1]. Step 1: 9+1+0=10, node 0, carry 1. Step 2: 9+0+1=10, node 0, carry 1. Step 3: both lists exhausted but carry is 1, so we create one more node: 1. Result: [0, 0, 1] = 100.

Different length lists are handled automatically by the algorithm because you treat a null node as having value 0. If l1 has 3 nodes and l2 has 1 node, after the first iteration l2 becomes null, and from then on y is always 0. No special code needed.

Other edge cases include: both lists have a single node (e.g., [5] + [5] = [0, 1]), one list is [0] (adding zero), and very long lists where carry cascades through many digits. The beauty of the linked list addition pattern is that the same loop handles all of these uniformly.

  • Final carry: 99 + 1 = 100 needs an extra node — the "while carry" condition handles this
  • Different lengths: treat null nodes as 0 — no special case needed
  • Single digits: [5] + [5] = [0, 1] (carry produces extra node)
  • Adding zero: [0] + [3, 2, 1] = [3, 2, 1] — works naturally
  • Cascading carry: [9, 9, 9] + [1] = [0, 0, 0, 1]
⚠️

Watch Out

Don't forget the final carry — if both lists are exhausted but carry is 1 (e.g., 5+5=10), you need one more node. The 'while carry' condition in the loop handles this.

What This Problem Teaches

Add Two Numbers is more than just a single problem — it teaches you the digit by digit addition pattern that appears across LeetCode in various forms. The same carry propagation logic applies to Add Binary (#67), where you add two binary strings bit by bit. It also appears in Plus One (#66), Multiply Strings (#43), and Add Strings (#415).

The dummy node technique you learn here is equally important. Any time you need to build a new linked list node by node, starting with a dummy eliminates awkward null checks for the first element. You will use this pattern in Merge Two Sorted Lists (#21), Partition List (#86), and Remove Nth Node From End of List (#19).

If you are preparing for coding interviews, add two numbers explained in this walkthrough covers two fundamental skills: linked list traversal with mutable state and building result lists with dummy nodes. Practice this problem until the pattern is automatic, then test yourself with the variations mentioned above. YeetCode flashcards can help you drill the pattern recognition — when you see "add numbers stored as lists," you should instantly think dummy node plus carry loop.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now