Skip to content
CodeShare
All problems

Add Two Numbers

Add two numbers stored as linked lists of digits.

MediumLinked listMath

You're given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, one digit per node: [2, 4, 3] is the number 342.

Add the two numbers and return the sum as a linked list in the same format. Lists are written as arrays in the examples.

Examples

Example 1
Input
l1 = [2, 4, 3], l2 = [5, 6, 4]
Output
[7, 0, 8]
Why
342 + 465 = 807.
Example 2
Input
l1 = [0], l2 = [0]
Output
[0]
Example 3
Input
l1 = [9, 9, 9, 9, 9, 9, 9], l2 = [9, 9, 9, 9]
Output
[8, 9, 9, 9, 0, 0, 0, 1]

Constraints

  • Each list has between 1 and 100 nodes.
  • 0 <= Node.val <= 9
  • The numbers have no leading zeros, except the number 0 itself.