Add Two Numbers
Add two numbers represented by linked lists in reverse order Example: [2,4,3], [5,6,4] => [7,0,8] (342 + 465 = 807)
Two non-empty linked lists each hold a non-negative integer, one digit per node, with the least significant digit first. Return the sum of the two numbers as a linked list in the same reverse-digit order. Neither input has leading zeros except the number 0 itself, and the result must not have any either. Part 2 returns the difference l1 - l2, given that l1 >= l2, and Part 3 returns the product.
Examples
You write
add_two_numbers(l1: Optional[ListNode], l2: Optional[ListNode])
Example 1
- Input
- l1 = [2,4,3], l2 = [5,6,4]
- returns
- [7,0,8]
Example 2
- Input
- l1 = [0], l2 = [0]
- returns
- [0]
The pattern behind it
Linked Lists
I reach for it when
- Nodes point to the next node, and you cannot index into the list.
- You must reverse, merge, reorder or remove a node.
Say this before you type. Say which pointers you hold and which link you are about to change.
All 12 Linked Lists problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.