Add Two Numbers II - Complete Solution Guide
Add Two Numbers II is LeetCode problem 445, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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 = [7,2,4,3], l2 = [5,6,4] Output: [7,8,0,7] Example 2: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [8,0,7] Example 3: Input: l1 = [0], l2 = [0] Output: [0] Constra
Detailed Explanation
The problem requires us to add two numbers represented as linked lists where each node contains a single digit and the most significant digit comes first. We need to return the sum as a new linked list, also with the most significant digit first. We are given that there are no leading zeros, except for the number 0 itself.
Solution Approach
The provided solution uses stacks to store the values of the linked lists in reverse order. This allows us to perform the addition from the least significant digit to the most significant digit. The algorithm iterates until both stacks are empty and there is no carry left. In each iteration, it pops a value from each stack (or uses 0 if the stack is empty), calculates the sum along with any carry from the previous step, and creates a new node with the digit to be added to the result list. The new node is prepended to the result linked list to build the sum in the correct order.
Step-by-Step Algorithm
- Step 1: Traverse the first linked list (l1) and push each node's value onto stack s1.
- Step 2: Traverse the second linked list (l2) and push each node's value onto stack s2.
- Step 3: Initialize carry to 0 and head to NULL (which will become the head of the resulting linked list).
- Step 4: While either stack s1 or s2 is not empty, or there is a carry, do the following:
- Step 5: Pop the top element from s1 (or 0 if s1 is empty) and assign it to val1.
- Step 6: Pop the top element from s2 (or 0 if s2 is empty) and assign it to val2.
- Step 7: Calculate the sum total = val1 + val2 + carry.
- Step 8: Calculate the digit = total % 10 (the remainder after division by 10).
- Step 9: Calculate the new carry = total // 10 (the integer part of the division by 10).
- Step 10: Create a new node with the calculated digit as its value.
- Step 11: Set the new node's next pointer to the current head.
- Step 12: Update the head to the new node.
- Step 13: After the loop finishes, return the head of the resulting linked list.
Key Insights
- Insight 1: Since we need to add the numbers starting from the most significant digit, we need a way to access the digits in reverse order. Using stacks is an efficient way to achieve this without modifying the original linked lists (as suggested in the follow-up).
- Insight 2: We need to simulate the addition process we do manually, handling the carry-over between digits.
- Insight 3: We need to consider the case where one list is longer than the other, and also the case where there is a carry left over after processing all digits.
Complexity Analysis
Time Complexity: O(max(n, m))
Space Complexity: O(n + m)
Topics
This problem involves: Linked List, Math, Stack.
Companies
Asked at: Juniper Networks.