Problem Statement

Add Two Numbers

You are given two linked lists. A linked list is a chain of little boxes called nodes, where each box holds one piece of data and a pointer to the next box. Here each box holds a single digit. The two lists stand for two numbers, but the digits are stored backwards: the first box is the ones digit, the next box is the tens digit, and so on. For example, the list 2 then 4 then 3 means the number 342. Your job is to add the two numbers and give back the answer as a new linked list, also stored backwards.

mediumLinked ListMathLinked ListTime: O(max(n, m)) · Space: O(max(n, m))

Signals to notice

add two numbers digit by digitlinked list representationcarry propagation

Brute force first

Convert to numbers, add, convert back — fails for very large numbers. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

Traverse both lists simultaneously, add digits with carry. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Trace it on l1=[2,4,3], l2=[5,6,4] (represents 342 + 465)

init: dummy=ListNode(), curr=dummy, carry=0, result=[]
step1: val1=2,val2=5 -> total=2+5+0=7, carry=0, append 7 -> result=[7]; advance l1=[4,3],l2=[6,4]
step2: val1=4,val2=6 -> total=4+6+0=10, carry=1, append 0 -> result=[7,0]; advance l1=[3],l2=[4]
step3: val1=3,val2=4 -> total=3+4+1=8, carry=0, append 8 -> result=[7,0,8]; advance l1=None,l2=None
loop check: l1=None, l2=None, carry=0 -> stop
return dummy.next = [7,0,8]  (807)

What must stay true

Process one digit at a time, propagating carry to the next position. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Shape of the loop

dummy = node(); curr = dummy; carry = 0
while l1 or l2 or carry:
    total = (l1.val or 0) + (l2.val or 0) + carry
    carry = total // 10
    curr.next = node(total % 10); curr = curr.next
    advance l1, l2 if present
return dummy.next

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Forgetting the final carry — if the last addition produces a carry, add an extra node. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Linked List Pattern