Loading...
You are given two lists of digits, a and b, each representing a non-negative integer with its digits stored in reverse order: the first node holds the ones digit, the next node holds the tens digit, and so on.
Add the two numbers and return the sum as a list of digits in the same reversed order.
Neither a nor b has a leading zero, unless the number it represents is 0, in which case the list holds a single 0 node.
Your function receives a and b as ListNode values and returns the head of the result list. The node type is provided for you, with val and next fields; you may build the result out of new nodes or reuse existing ones.
The examples below write a list as the array of its digit values in order, so [2,4,3] is the list 2 -> 4 -> 3, representing the number 342. That is only how lists are displayed; the encoding and decoding are done for you.
Input: a = [2,4,3], b = [5,6,4]
Output: [7,0,8]
Explanation: a is 342, b is 465, and 342 + 465 = 807, stored in reverse order as 7 -> 0 -> 8.
Input: a = [0], b = [0]
Output: [0]
Explanation: 0 + 0 = 0.
Input: a = [9,9,9,9,9,9,9], b = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Explanation: a is 9999999, b is 9999, and 9999999 + 9999 = 10009998, stored in reverse order as 8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1.
a and in b is each in the range [1,100].a[i], b[i] ≤9.a nor b has a leading zero, unless the number it represents is 0 (a single 0 node).