Add two numbers stored in lists
Mediumlinked listmath
Each list represents a non-negative number with its digits stored in reverse (ones digit first), one digit per node. Add the two numbers and return the sum in the same form.
Example 1
Input: a = [2,4,3], b = [5,6,4] Output: [7,0,8]
342 + 465 = 807.
Example 2
Input: a = [9,9,9,9], b = [9,9] Output: [8,9,0,0,1]
Constraints
- 1 ≤ nodes in each list ≤ 100
- 0 ≤ Node.val ≤ 9
- No leading zeros except the number 0 itself
Submit checks your solution against 9 tests, including hidden ones.
// Provided by the judge: keep these definitions
class ListNode {
constructor(val = 0, next = null) {
this.val = val
this.next = next
}
}
/**
* @param {ListNode|null} a
* @param {ListNode|null} b
* @return {ListNode|null}
*/
function addTwoNumbers(a, b) {
}