Add Two Numbers

MediumLinked List · Digit Math🧩 Linked List TechniquesLeetCode #2 ↗Updated

Problem

Two non-empty linked lists each encode a non-negative integer, one digit per node, with the least significant digit first. Return their sum as a new linked list in the same reversed-digit format. Neither number has leading zeros (except 0 itself).
Input[2,4,3] + [5,6,4]Output[7,0,8] (342+465)
Input[9,9,9,9] + [9,9]Output[8,9,0,0,1]
Input[0] + [0]Output[0]

1 ≤ nodes per list ≤ 100  ·  0 ≤ Node.val ≤ 9  ·  no leading zeros

Try it yourself

Java
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
The digits already come units first — exactly the order grade-school addition works in. Walk both lists together, add digit + digit + carry, emit sum % 10, carry sum / 10.
PatternLinked List Techniques

Instinct Nodes joined by next pointers and the task is to reverse, splice, merge, reorder or remove in place - grab a dummy head and rewire pointers carefully.

Memory hack Save next, flip, step - never let go of a rope before you've grabbed the next one.

Open the Linked List Techniques guide →

Approaches

Brute ForceConvert to BigInteger and Back

Time O((m+n)2)Space O(m+n)

Read each list into a string (reversed), turn it into a number, add, then write the digits of the result back out units-first. A long overflows at 19 digits and lists hold up to 100, so BigInteger is required — and its decimal parse/print is quadratic in the digit count.

Java
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    java.math.BigInteger sum = toNumber(l1).add(toNumber(l2));
    String s = sum.toString();
    ListNode dummy = new ListNode(0), cur = dummy;
    for (int i = s.length() - 1; i >= 0; i--) {
        cur.next = new ListNode(s.charAt(i) - '0');
        cur = cur.next;
    }
    return dummy.next;
}
private java.math.BigInteger toNumber(ListNode node) {
    StringBuilder sb = new StringBuilder();
    for (; node != null; node = node.next) sb.append(node.val);
    return new java.math.BigInteger(sb.reverse().toString());
}

Works, but leans on a big-number library and does far more work than needed.

At a glance

LevelApproachTimeSpace
Brute ForceConvert to BigInteger and BackO((m+n)2)O(m+n)
BetterRecursion with CarryO(max(m,n))O(max(m,n))
OptimalIterative Digit Addition (Dummy Head)O(max(m,n))O(1) extra
Common pitfalls
  • Converting to int/long — overflows for 100-digit numbers.
  • Dropping the final carry — [5]+[5] must give [0,1], not [0].
  • Stopping when the shorter list ends instead of treating missing digits as 0.
Interview tips
  • Point out the reversed order is a gift: it matches how carries propagate.
  • Use a dummy head and say why: no null check for the first node.
  • Clarify whether you may reuse input nodes; the safe default is a new list.
Pattern
Simultaneous traversal + carry → arithmetic on numbers stored as digit sequences.
My notes saved in this browser