Add Two Numbers
Updated
Problem statement
You are given two non-empty linked lists, l1 and l2. Each list stores a non-negative integer, one decimal digit per node. The digits are stored in reverse order: the first node holds the ones digit, the second node holds the tens digit, and so on.
Add the two numbers and return the sum as a new linked list in the same format: one digit per node, ones digit first.
Neither number has leading zeros, except the number 0 itself, which is a list with one node holding 0.
The examples write each list as its node values from the first node to the last.
Function
addTwoNumbers(l1: ListNode, l2: ListNode) → ListNodeExamples
Example 1
l1 = [4,8,6]l2 = [7,5]return = [1,4,7]Read from the last node back to the first, l1 holds 684 and l2 holds 57. Their sum is 741, so the answer lists its digits ones digit first: [1,4,7]. Digit by digit: 4 + 7 = 11 gives digit 1 and a carry of 1; 8 + 5 + 1 = 14 gives 4 and a carry of 1; l2 has no third node, so the last step is 6 + 1 = 7, which gives 7.
Example 2
l1 = [9,9]l2 = [3]return = [2,0,1]l1 holds 99 and l2 holds 3, and 99 + 3 = 102. The first position gives 9 + 3 = 12: digit 2 and a carry of 1. l2 has no second node, so the second position gives 9 + 1 = 10: digit 0 and a carry of 1. The carry left after the last position becomes a third node holding 1.
Example 3
l1 = [0]l2 = [0]return = [0]Both lists hold the number 0, and 0 + 0 = 0. The sum is written as one node holding 0.
Constraints
- Each list has between
1and100nodes. 0 <= Node.val <= 9- Neither list has leading zeros: its last node is not
0unless the list is[0].
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Add Two Numbers IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Add Forward Linked-List Numbers Using RecursionPeopleStrong · PHONE SCREENPeopleStrong interview questions
- Subtract Forward-Order Linked-List NumbersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Add Two Numbers Stored in Reversed ListsExpedia · ONSITE INTERVIEWExpedia interview questions
- Add Numbers Stored in Four-Digit ChunksZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
- Add Two Numbers Stored as Linked ListsCapital One · ONSITE INTERVIEWCapital One interview questions
- Add NumbersCisco · OACisco interview questions
- Binary Linked List to DecimalJP Morgan Chase · OAJP Morgan Chase interview questions
- Path Between Two Nodes of a Fibonacci TreeDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions