Add Two Numbers as Lists
mediumA big-integer library stores numbers as linked lists of decimal digits in reverse order — least significant digit first — because that's the order addition consumes them. 342 is stored as 2 → 4 → 3.
Given two non-negative integers in this format, return their sum as a new list in the same format. The numbers can be far larger than any built-in integer width is meant to flex — work digit by digit with a carry. Implement _add(a, b) on real nodes.
Example 1
in a = [2, 4, 3], b = [5, 6, 4]
out [7, 0, 8]
342 + 465 = 807, stored reversed.
Example 2
in a = [0], b = [0]
out [0]
Example 3
in a = [9, 9, 9, 9, 9, 9, 9], b = [9, 9, 9, 9]
out [8, 9, 9, 9, 0, 0, 0, 1]
9,999,999 + 9,999 = 10,009,998 — the carry ripples and extends the result.
Constraints
- · 1 <= nodes in each list <= 100
- · 0 <= digit <= 9
- · no leading zeros except the number 0 itself
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.