Add Two Numbers

Medium · Linked Lists · Linked List · Math · Recursion

Problem

Two non-negative integers are stored as linked lists with their digits in reverse order — so 342 is 2 -> 4 -> 3. Each node holds one digit.

Add the numbers and return the sum as a linked list in the same reversed form.

from_list() and to_list() are provided.

Examples

Input: l1 = 2 -> 4 -> 3, l2 = 5 -> 6 -> 4
Output: 7 -> 0 -> 8

342 + 465 = 807.

Input: l1 = 0, l2 = 0
Output: 0
Input: l1 = 9 -> 9 -> 9, l2 = 1
Output: 0 -> 0 -> 0 -> 1

999 + 1 = 1000; the carry adds a node.

Constraints

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…