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
- 1 <= nodes in each list <= 100
- 0 <= Node.val <= 9
- No leading zeros except the number 0 itself
Approaches
- Convert to integers and back — time O(m + n), space O(m + n)
Related problems
Start free on Cariara · Coding problems · System design studio