> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Add Two Numbers II Python Solution with Tests

> Tested Python solution for LeetCode 445 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 445, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Math](/catalog/topics/math), [Stack](/catalog/topics/stack). [View on LeetCode](https://leetcode.com/problems/add-two-numbers-ii/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 445   # by problem number
lcpy gen -s add_two_numbers_ii   # by problem name
```

## Problem

You are given two **non-empty** linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number `0` itself.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg)

```
Input: l1 = [7,2,4,3], l2 = [5,6,4]
Output: [7,8,0,7]
```

```
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [8,0,7]
```

```
Input: l1 = [0], l2 = [0]
Output: [0]
```

### Constraints

* The number of nodes in each linked list is in the range `[1, 100]`.
* `0 <= Node.val <= 9`
* It is guaranteed that the list represents a number that does not have leading zeros.

**Follow up:** Could you solve it without reversing the input lists?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_two_numbers_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/add_two_numbers_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(max(m, n))
    # Space: O(m + n)
    def add_two_numbers(
        self, l1: ListNode[int] | None, l2: ListNode[int] | None
    ) -> ListNode[int] | None:
        stack1: list[int] = []
        stack2: list[int] = []
        while l1:
            stack1.append(l1.val)
            l1 = l1.next
        while l2:
            stack2.append(l2.val)
            l2 = l2.next

        carry = 0
        head: ListNode[int] | None = None
        while stack1 or stack2 or carry:
            digit_sum = carry
            if stack1:
                digit_sum += stack1.pop()
            if stack2:
                digit_sum += stack2.pop()
            carry, digit = divmod(digit_sum, 10)
            head = ListNode(digit, head)
        return head
```

## Complexity

| Time | Space |
| - | - |
| O(max(m, n)) | O(m + n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.