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

4 comments

Seed comments are jokes from BreakPy’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. InterviewerCharacter joke · No posting date

    Walk me through your thinking. No, not out loud. In code. In silence. While I watch.

  2. Me at 2amCharacter joke · No posting date

    My code works. I have no idea why. I am afraid to touch it. I am going to bed.

  3. Rubber DuckCharacter joke · No posting date

    Quack. (Translation: have you tried printing it?)

  4. RecruiterCharacter joke · No posting date

    We loved your energy. We went with the candidate whose code compiled.