Add Two Numbers

Free with account Updated Nov 19, 2026 · 5 min read
On this page

    Problem statement

    Two non-negative integers are stored as linked lists of digits, lowest digit first. Return their sum as a list in the same format. Each node holds one digit 0–9. The numbers have no leading zeros, except the number 0 itself, which is a single node [0].

    A classic warm-up that still filters out many candidates: the logic is grade-school addition, and the score comes from handling the carry and lists of different lengths without special cases. Original problem: LeetCode 2.

    Example

    642 is stored as 2 → 4 → 6 and 879 as 9 → 7 → 8. Their sum 1521 comes back as 1 → 2 → 5 → 1, one node longer than either input.
    642 is stored as 2 → 4 → 6 and 879 as 9 → 7 → 8. Their sum 1521 comes back as 1 → 2 → 5 → 1, one node longer than either input.
    Input:  l1 = [2, 4, 6], l2 = [9, 7, 8]      # 642 + 879
    Output: [1, 2, 5, 1]                        # 1521
    Input:  l1 = [9, 9], l2 = [1]               # 99 + 1
    Output: [0, 0, 1]                           # 100

    Constraints (assumed)

    • Each list has 1 to 100 nodes, so the numbers can be far larger than 64 bits.
    • Node values are 0–9.

    What this question tests

    • Walking two lists of different lengths in one loop.
    • Carrying correctly, including the carry left over at the end.
    • Using a dummy head so the first node needs no special case.

    Questions to ask first

    1. Are the digits stored lowest first? (Here: yes. The reversed order is a separate variant.)
    2. Can I convert the lists to integers? Not in languages with fixed-size integers, since 100 digits overflow; and it misses the point anyway.
    3. May I modify the input lists, or must I build a new one? (Here: build a new one.)
    4. Can the lists be empty? (Here: no, each has at least one node.)