Add Two Numbers
Two non-empty lists store digits in reverse order, one digit per node. Add the numbers and return the sum as a list, also in reverse order.
Open on LeetCode ↗Intuition
Reverse order is a gift: the heads are the ones digits, so you can add left to right exactly like grade-school addition, carrying into the next node as you go.
Approach
Converting to integers doesn't scale
It's tempting to read each list into a number, add them, and rebuild — but the lists can be hundreds of digits long, far past what a native integer holds, and rebuilding throws away the very alignment the problem hands us. Better to add the lists directly, digit by digit.
Reverse storage means add left to right
The digits are stored in reverse order, so the heads are the ones place. That's a gift: it lets us add exactly the way we do by hand, starting from the least significant digit. Walk both lists in lockstep; at each position the column total is a + b + carry, the new digit is total % 10, and the carry into the next column is total // 10.
One loop, driven by lists-or-carry
Use a dummy head and append one result node per column. Loop while either list still has nodes or a carry remains — that last condition is what creates the extra leading digit in cases like 5 + 5 = 10. Treat a missing node as 0 so lists of different lengths just contribute zeros once exhausted. O(max(m,n)) time and space.
Solution & live demo
Edge cases
Treat a missing node as 0, so the shorter list simply contributes zeros once exhausted.
The loop condition includes carry, so a trailing carry creates the extra high-order node.
0 + 0 = 0 with no carry; one result node holding 0 is produced.