Add Two Numbers
Part of Practice: Gaps, Clones & Carries
Two non-negative integers are each stored as a linked list of their digits in reverse order — the ones digit first. So 342 is stored as 2 -> 4 -> 3. Given the heads of two such lists, add the numbers and return the sum as a new list in the same reversed-digit format.
The lists are non-empty, may have different lengths, and have no leading zeros (except the number 0 itself). Each list can hold up to 100 digits, so the numbers can be far too large for built-in integer types — you must add digit by digit.
Examples
- Input:
- l1 = [2, 4, 3], l2 = [5, 6, 4]
- Output:
- [7, 0, 8]
342 + 465 = 807, stored ones-first as 7 -> 0 -> 8.
- Input:
- l1 = [0], l2 = [0]
- Output:
- [0]
- Input:
- l1 = [9, 9, 9, 9, 9, 9, 9], l2 = [9, 9, 9, 9]
- Output:
- [8, 9, 9, 9, 0, 0, 0, 1]
9,999,999 + 9,999 = 10,009,998; note the final carry creates an extra digit.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.