Why it works
- The lists start from the ones digit, so walking them adds column by column, like on paper: each column gives one digit, sum mod 10, and a carry, floor(sum / 10).
- A list that has run out adds 0, so numbers of different lengths need no special case.
- The loop also runs while carry is more than 0: after the last digits are added, the carry still becomes a new node, here the 1 of 1043.
Complexity
Time O(max(n, m)): one step per column. Memory O(max(n, m)) for the answer list, O(1) besides it.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗