Add Two Numbers Stored in Reversed Lists
Updated
Problem statement
The linked lists first and second each hold a whole number that is 0 or larger. Each node holds one digit, and the digits go from the ones place upward. For example, the list [3,4,2] stores the number 243.
Build and return a linked list for their total, written the same way: one digit per node, ones digit first.
Neither number has leading zeros, so the last node of a list is never 0 unless the whole list is the single node 0. Your answer must follow the same rule. For example, a sum of zero is returned as [0].
Function
addTwoNumbers(first: ListNode, second: ListNode) → ListNodeExamples
Example 1
first = [3,4,2]second = [8,6]return = [1,1,3]first stores 243 and second stores 68. Their sum is 243 + 68 = 311, which is stored as [1,1,3].
Digit by digit: 3 + 8 = 11, so write 1 and carry 1. Then 4 + 6 + 1 = 11, so write 1 and carry 1. Then 2 + 1 = 3, so write 3.
Example 2
first = [9,9,9]second = [1]return = [0,0,0,1]999 + 1 = 1000. The carry moves through every digit of first and is still 1 at the end, so the answer gets one more node: [0,0,0,1].
Example 3
first = [0]second = [0]return = [0]0 + 0 = 0, which is the single node [0].
Constraints
- Each list has between
1and100nodes. - Every node value is a digit from
0to9. - The last node of each list is not
0, unless the list is the single node0.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Add Two Numbers IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Subtract Forward-Order Linked-List NumbersMicrosoft · ONSITE INTERVIEWMicrosoft 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 Forward Linked-List Numbers Using RecursionPeopleStrong · PHONE SCREENPeopleStrong interview questions
- Add NumbersCisco · OACisco interview questions
- Binary Linked List to DecimalJP Morgan Chase · OAJP Morgan Chase interview questions
- Integers With Multiple Sum of Two CubesCharta Health · PHONE SCREEN · ONSITE INTERVIEWCharta Health interview questions
- Reverse a Sublist of a Linked ListSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions