Add Two Numbers
Detailed guide and Python implementation for the 'Add Two Numbers' problem.
Problem Statement
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, 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.
The linked lists are represented as Python lists. Implement a function addTwoNumbers(l1: list, l2: list) -> list that returns the sum as a list in reverse digit order.
- •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
Examples
[2,4,3], [5,6,4]
[7,0,8]
342 + 465 = 807. Represented in reverse: [7,0,8].
[0], [0]
[0]
0 + 0 = 0.
[9,9,9,9,9,9,9], [9,9,9,9]
[8,9,9,9,0,0,0,1]
9999999 + 9999 = 10009998. Represented in reverse: [8,9,9,9,0,0,0,1].
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Add and Update Keys in a Python Dictionary
Learn how to add elements or update key-value pairs in a Python dictionary. Explore square brackets, update method, and merge operator options.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.