Back

Add Two Numbers

medium

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, also in reverse order.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Test Cases

Copy an input into the main harness and Run to verify
Input
l1 = [2,4,3], l2 = [5,6,4]
Expected Output
[7,0,8]

Explanation: 342 + 465 = 807, stored in reverse as [7,0,8].

Input
l1 = [0], l2 = [0]
Expected Output
[0]
Input
l1 = [9,9,9,9], l2 = [9,9]
Expected Output
[8,9,0,0,1]

Explanation: 9999 + 99 = 10098.

Constraints

  • The number of nodes in each list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • The lists represent numbers with no leading zeros.

Hints

Hint 1 — click to reveal

Reverse order is a gift — it means you start at the least significant digit, exactly where addition begins.

Hint 2 — click to reveal

Do not forget a final carry after both lists are exhausted.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.