Skip to content
Onsite

Add Two Linked Lists

MediumP1Coding

Two non-negative integers are stored as singly linked lists of digits in reverse order: the head is the ones digit, the next node the tens digit, and so on. Each node holds a single digit. Add the two numbers and return the sum as a linked list in the same format.

A list node has val and next. The ListNode class is already defined for you. Neither number has leading zeros, except the number 0 itself.

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]
Why
9999999 + 9999 = 10009998.

Constraints

  • 1 <= length of each list <= 100
  • 0 <= Node.val <= 9
  • No leading zeros except the number 0.

Hints

Run checks the 3 visible tests, Submit all 8.