Add Two Numbers

Med
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Add Two Numbers

Approach

Use a dummy node to build the result list. Traverse both input lists simultaneously, summing corresponding digits plus any carry from the previous column. Create a new node for each digit and propagate the carry until both lists and the carry are exhausted.

Complexity Analysis

Time
O(max(n, m))
Space
O(max(n, m))

Pattern

Linked List (Dummy Node + Carry)

Why It Works

Since digits are stored in reverse order, the head of each list is the ones place, which is exactly where addition starts. Processing left to right mirrors grade-school addition from least significant to most significant digit.

Updated Feb 2026