Add Two Numbers
medium · digit addition with carry
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, so the first node holds the ones digit, and each list contains no leading zeros except the number 0 itself, represented as a single node with value 0. Add the two numbers and return the sum as a linked list in the same reversed-digit format. Do not convert the lists to integers — the numbers can be far too large. The nodes are ListNode objects with val and next.
Constraints
Each list has 1–100 nodes; digits 0–9.
Examples
[2,4,3]+[5,6,4] → [7,0,8] (342+465=807)
[0]+[0] → [0]
[9,9,9,9,9,9,9]+[9,9,9,9] → [8,9,9,9,0,0,0,1] (9,999,999+9,999=10,009,998)
Starter code
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def add_two_numbers(l1: ListNode | None, l2: ListNode | None) -> ListNode | None:
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter