00:00

Problem Statement

Input/Output Examples

Visual Explanation

The loop continues while either list has digits or a carry remains, which handles different lengths and a final carry with the same code.

Approaches

Both approaches add digit by digit. One loops with a dummy head; the other recurses with the carry as a parameter.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n and m are the list lengths. Each digit position is processed once; the result has at most one more digit than the longer list.

Interview Tips and Recommended Approach

  • Keep looping while there is a carry. Forgetting it turns 99 + 1 into 00.
  • Do not convert to integers. Lists can hold numbers far larger than long.
  • Use a dummy head to avoid special-casing the first digit.
  • Know the forward-order variant. With the most significant digit first, reverse the lists or use stacks.

Recommended Approach: Iterative With Carry

Why: it mirrors paper addition, handles any length and needs only one loop and a carry variable.