
Problem
Solution
Submissions
Add Two Numbers
Certification: Intermediate Level
Accuracy: 50%
Submissions: 4
Points: 10
Write a C program to add two numbers represented by linked lists. The digits are stored in reverse order, and each node in the linked list contains a single digit. You need to add the two numbers and return the sum as a linked list. 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: The numbers represented by the linked lists are 342 and 465 (in reverse order). 342 + 465 = 807 The result 807 is represented as [7,0,8] in reverse order.
Example 2
- Input: l1 = [0], l2 = [0]
- Output: [0]
- Explanation: Both linked lists represent the number 0. 0 + 0 = 0 The result 0 is represented as [0] in the output linked list.
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
- The two linked lists may have different lengths
- You must solve the problem without modifying the input linked lists
- Time Complexity: O(max(m,n)), where m and n are the lengths of the two lists
- Space Complexity: O(max(m,n)), where m and n are the lengths of the two lists
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Traverse both linked lists simultaneously and add the digits at the corresponding positions
- Keep track of the carry value when the sum of two digits exceeds 9
- Create a new linked list to store the result
- Continue the traversal until both linked lists are completely traversed
- Remember to handle the carry value after traversing both lists