Link: https://leetcode.com/problems/add-two-numbers/
Solution:
Topics: linked list, math
Intuition
Simple little problem, but a good exercise in modulus and integer division. The code more or less writes itself, but the key idea is to compute how much to carry with total//10
, and then masking out the tens with total%10
.
I spent a minute deciding whether or not to solve this in-place or create a new list…I decided that in-place would introduce too many edge cases. The editorial implementation agrees with me, also in general the output doesn’t count towards the memory complexity so we can still call this constant memory.
Implementation
Review 1
Cute problem. I like the above implementation. Very concise.