You get two linked lists, list1 and list2. Each one holds a whole number (0 or more), one digit per node.
The digits are stored starting with the ones digit. For example, 472 is the list 2 -> 7 -> 4.
Return the sum of the two numbers as a new list, stored the same way.
Neither number has extra zeros in front. So a list ends in 0 only when it is just [0].
Example 1:
Input: list1 =[5,8,1], list2 =[7,3]Output:[2,2,2]Explanation: The numbers are 185 and 37. 185 + 37 = 222. Ones: 5 + 7 = 12. Write 2 and carry 1. Tens: 8 + 3 + 1 = 12. Write 2 and carry 1. Hundreds: 1 + 1 = 2.
Example 2:
Input: list1 =[9,9], list2 =[1]Output:[0,0,1]Explanation: 99 + 1 = 100. The last carry needs a node of its own.
Example 3:
Input: list1 =[0], list2 =[4,3]Output:[4,3]Explanation: 0 + 34 = 34.
Constraints:
- Each list has 1 to 100 nodes.
- Each node holds one digit, from 0 to 9.
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. PRO is unlimited.
