Add Two Numbers
Sum two integers represented as reversed digit linked lists and return the result as a linked list.
What This Problem Is Asking
In one sentence
Walk both lists from the least significant digit, propagate carry, and build the result list.
Full problem statement
Add Two Numbers
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 in the same reverse-digit form.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Recognition Clues
Signals that this pattern likely applies.
- Two sequences are consumed in parallel with shared carry state.
- Lists may differ in length; a final carry can add an extra digit.
- Each step only needs the current nodes and the running carry.
Why This Pattern Fits
Two pointers plus carry mimic manual addition and avoid converting whole numbers to primitives.
Daily usefulness: Same step-and-carry idea appears when merging streams or simulating arithmetic on chunked data.
Interview usefulness: Common screen for pointer discipline, edge cases, and dummy-node list construction.
Real-world relevance: Aligns with big-integer APIs, ledger rounding, and incremental numeric pipelines.
Code Snippets
1/**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode() : val(0), next(nullptr) {}
7 * ListNode(int x) : val(x), next(nullptr) {}
8 * ListNode(int x, ListNode *next) : val(x), next(next) {}
9 * };
10 */
11class Solution {
12public:
13 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14 // dummy = a placeholder node so we don’t have to handle
15 // "first node" as a special case
16 ListNode* dummy = new ListNode(0);
17
18 // current = pointer we use to build the new linked list
19 ListNode* current = dummy;
20
21 // carry = stores extra when sum is 10 or more
22 // Example: 8 + 7 = 15 → store 5, carry 1
23 int carry = 0;
24
25 // Keep looping while:
26 // - l1 still has nodes
27 // - l2 still has nodes
28 // - or we still have a carry
29 while (l1 != nullptr || l2 != nullptr || carry != 0) {
30
31 // start with carry from previous step
32
33 int sum = carry;
34
35 // if l1 exists, add its value
36 if (l1 != nullptr) {
37 sum += l1->val;
38
39 l1 = l1->next;
40 }
41
42 // if l2 exists add it's value
43 if(l2 != nullptr){
44 sum += l2->val;
45 l2 = l2->next;
46 }
47
48 // digit = what we store in this node (ones place)
49 int digit = sum % 10;
50
51 // carry = what we pass to the next interation
52 int newCarry = sum / 10;
53
54 // Create a new node with the digit
55 current->next = new ListNode(digit);
56
57 // move current forward
58 current = current->next;
59
60 // Update carry for next loop
61 carry = newCarry;
62
63
64
65 }
66 //dummy was just a helper
67 // return the real head of the result
68 return dummy->next;
69 }
70};Practical Use Cases
- Combine chunked numeric payloads where full conversion to a scalar is unsafe or undesirable.
- Simulate addition in finance or crypto tooling that uses digit or limb sequences.
- Build teaching demos of arbitrary-precision arithmetic from simple list nodes.
Demo Ideas
Interactive ways to teach this algorithm.
- Step through each pair of digits with an animated carry chip.
- Toggle unequal-length lists to show how the shorter stream is “zero” after it ends.