Example 1
ready- Input
l1 = [ 2, 4, 3 ] l2 = [ 5, 6, 4 ]- Expected
[ 7, 0, 8 ]- Why
- 342 + 465 = 807, stored ones digit first
These numbers greet you backwards: the ones digit arrives first, followed by the tens, hundreds, and everything beyond.
Each of l1 and l2 is a reverse-order digit list — a chain with one decimal digit
per node, next links between nodes, and its ones digit at the head. The lists represent
two non-negative integers. Add them and return their sum in the same reverse-order list
format.
A few ground rules:
0 through 9.[0].1 <= l1.length, l2.length <= 1000 <= Node.val <= 90.Hints
Because the ones digits come first, you can add in the same direction that you walk the lists. There's no reason to reverse either input.
At each position, add the two available digits and the carry from the previous
position. Store total % 10; carry total // 10 into the next round.
When one list ends, treat its missing digit as zero. Keep going while either list has a node or the carry is nonzero — that last condition creates a new leading digit when the sum grows.
How would your approach change if the most significant digit came first and you weren't allowed to reverse either input list?
Visible cases
l1 = [
2,
4,
3
]
l2 = [
5,
6,
4
][
7,
0,
8
]l1 = [
0
]
l2 = [
0
][
0
]l1 = [
9,
9,
9,
9
]
l2 = [
1
][
0,
0,
0,
0,
1
]Interview signal
The direct route is to reconstruct both whole numbers, let Python add them, then peel digits from the sum to build the returned list. Python integers can grow beyond the machine's fixed-width limit, so this works for every input here.
def read_number(head: ListNode | None) -> int:
value = 0
place = 1
current = head
while current is not None:
value += current.val * place
place *= 10
current = current.next
return value
def add_two_numbers(
l1: ListNode | None,
l2: ListNode | None,
) -> ListNode | None:
total = read_number(l1) + read_number(l2)
if total == 0:
return ListNode(0)
dummy = ListNode()
tail = dummy
while total > 0:
total, digit = divmod(total, 10)
tail.next = ListNode(digit)
tail = tail.next
return dummy.nextThis baseline depends on arbitrary-size integers, which some languages don't provide as a built-in type. It also stores numbers whose digit count grows with the input.
Time O(m + n) — read both lists and emit the sum's digits. Space O(m + n) for the whole-number representation and returned list.
A carry is the tens part saved for the next column after one digit column is added.
The input order is already perfect for grade-school addition: both heads are the ones
column, and every next step moves to the next larger place.
For each column, use zero when a list has ended. The new digit is total % 10, and the
next carry is total // 10. Continue while a node or carry remains.
def add_two_numbers(
l1: ListNode | None,
l2: ListNode | None,
) -> ListNode | None:
dummy = ListNode()
tail = dummy
carry = 0
first, second = l1, l2
while first is not None or second is not None or carry:
total = carry
if first is not None:
total += first.val
first = first.next
if second is not None:
total += second.val
second = second.next
carry, digit = divmod(total, 10)
tail.next = ListNode(digit)
tail = tail.next
return dummy.nextAfter each round, the result contains every finished low-order digit, and carry holds
the only unfinished part of their sum. That invariant — a fact that stays true after
every round — covers unequal lengths and a carry chain through every remaining 9.
Time O(max(m, n)) — one visit per input node, plus at most one final carry node. Space O(1) beyond the result list, which is required output.
Ready to run.
3 cases are queued.
Visible testcase
Case 1
Expected
[ 7, 0, 8 ]