2. Add Two Numbers
Add Two Numbers (LeetCode 2) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Linked List Manipulation Patterns pattern, in the Addition of Numbers subpattern.
How to solve Add Two Numbers
Add Two Numbers is a Add Numbers Using Linked Lists problem, part of Linked List Manipulation Patterns. Master adding numbers represented as linked lists. Handle carry operations and different list lengths.
Work towards the solution in these steps:
- Consider traversing both linked lists simultaneously to process corresponding digits.
- Think about how to handle the addition digit-by-digit, similar to how you add numbers on paper starting from the least significant digit.
- Remember to keep track of the carry when the sum of two digits exceeds 9.
The reference solution runs in O(n) time and O(1) space.
The Add Two Numbers solution and editorial walks through the full approach with reference code.
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.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0] Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1]
Constraints:
- 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.
Topics and companies
Topics: Linked List, Math, Recursion.
Reported in interviews at Bloomberg, Microsoft, Amazon, Google, Facebook, Apple, Adobe, Paypal, Coupang, Oracle, Uber, VMware and others.
How to practise Add Two Numbers on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Add Two Numbers in the code editor, or read the Add Two Numbers editorial for a worked solution with its approach and complexity analysis.
Where Add Two Numbers sits in the DSA pattern sheet
- Linked List Manipulation Patterns pattern guide
- Addition of Numbers subpattern
- The full DSA patterns sheet
Problems related to Add Two Numbers
Other problems that use the same Addition of Numbers and Linked List Manipulation Patterns techniques:
- Plus One Linked List — Medium, same subpattern
- Insert Greatest Common Divisors in Linked List — Medium, same pattern
- Intersection of Two Linked Lists — Easy, same pattern
- Merge Two Sorted Lists — Easy, same pattern
- Minimum Index Sum of Two Lists — Easy, same pattern
- Odd Even Linked List — Medium, same pattern
Add Two Numbers FAQ
What is the LeetCode number of Add Two Numbers?
Add Two Numbers is LeetCode problem 2, rated Medium.
What pattern is used to solve Add Two Numbers?
Add Two Numbers is a Add Numbers Using Linked Lists problem. It belongs to the Linked List Manipulation Patterns pattern, in the Addition of Numbers subpattern.
What is the time complexity of Add Two Numbers?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Add Two Numbers?
Add Two Numbers has been reported in interviews at Bloomberg, Microsoft, Amazon, Google, Facebook, Apple, Adobe, Paypal, Coupang, Oracle, Uber, VMware and others.