Leetcode 445: Add Two Numbers II
You are given two non-empty linked lists where each node contains a single digit representing a non-negative integer. Add the two numbers and return the sum as a linked list, ensuring the most significant digit is at the head of the list.
Problem
Approach
Steps
Complexity
Input: Each linked list represents a number where each node contains a single digit. The lists do not have leading zeros, except for the number 0 itself.
Example: [3,4,2], [6,5,7]
Constraints:
• 1 <= Number of nodes in each linked list <= 100
• Node.val >= 0 and Node.val <= 9
• No leading zeros in the numbers except for 0 itself
Output: The output should be a linked list representing the sum of the two numbers with the most significant digit at the head.
Example: [9,0,0,9]
Constraints:
• The result linked list must be formatted in the same way as the input.
Goal: Add two numbers represented by two linked lists and return the sum as a linked list.
Steps:
• 1. Use two stacks to store the digits of the linked lists as we traverse them.
• 2. Pop the digits from the stacks, add them with the carry, and create new nodes to store the result.
• 3. If a carry exists after processing both lists, create a new node for the carry.
• 4. Ensure that the final linked list is built without reversing the input lists.
Goal: The linked lists represent valid numbers without leading zeros except 0 itself.
Steps:
• 1 <= Number of nodes in each linked list <= 100
• 0 <= Node.val <= 9
• Do not reverse the input linked lists.
Assumptions:
• The linked lists are not empty and contain valid digits.
• Input: [3,4,2], [6,5,7]
• Explanation: The two numbers are 342 and 657. Their sum is 999, which is represented as [9, 0, 0, 9].
• Input: [1,2], [3,4,5]
• Explanation: The two numbers are 12 and 345. Their sum is 357, represented as [3, 5, 7].
Approach: This problem can be solved by simulating the addition process using stacks to reverse the linked lists and adding corresponding digits along with a carry.
Observations:
• The problem asks to avoid reversing the linked lists, which suggests using a stack to reverse the order temporarily.
• We can use two stacks to simulate the addition process from the most significant digit to the least significant digit.
Steps:
• 1. Create two stacks to store the digits of both linked lists.
• 2. Traverse both lists, pushing each digit onto its respective stack.
• 3. Pop digits from both stacks, add them with the carry, and store the result in a new linked list.
• 4. If there's any carry left after processing both lists, create a new node for the carry.
• 5. Return the result as the new linked list.
Empty Inputs:
• If any linked list is empty, it should be treated as 0.
Large Inputs:
• For larger inputs (up to 100 nodes), ensure that the solution handles the carry correctly and efficiently.
Special Values:
• Consider cases where the numbers involve carry overs, such as adding [9,9] and [1].
Constraints:
• Ensure that the algorithm runs in O(n) time and uses O(n) space for stack operations.
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> s1, s2;
while(l1) {
s1.push(l1->val);
l1 = l1->next;
}
while(l2) {
s2.push(l2->val);
l2 = l2->next;
}
int cry = 0;
ListNode* node = NULL, *prv = NULL;
while(!s1.empty() || !s2.empty()) {
if(!s1.empty()) {
cry += s1.top();
s1.pop();
}
if(!s2.empty()) {
cry += s2.top();
s2.pop();
}
node = new ListNode(cry % 10);
node->next = prv;
prv = node;
cry /= 10;
}
if(cry != 0) {
node = new ListNode(cry %10);
node->next = prv;
}
return node;
}
1 : Function Definition
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
This is the function definition where two linked lists (l1 and l2) are passed as arguments.
2 : Stack Initialization
stack<int> s1, s2;
Two stacks are created to hold the digits of the two linked lists.
3 : Stack Population (L1)
while(l1) {
Traverse the first linked list and push each node's value onto stack s1.
4 : Push Operation
s1.push(l1->val);
Push the current value of the node in l1 onto stack s1.
5 : Linked List Traversal
l1 = l1->next;
Move to the next node in the first linked list.
6 : Stack Population (L2)
while(l2) {
Traverse the second linked list and push each node's value onto stack s2.
7 : Push Operation
s2.push(l2->val);
Push the current value of the node in l2 onto stack s2.
8 : Linked List Traversal
l2 = l2->next;
Move to the next node in the second linked list.
9 : Carry Initialization
int cry = 0;
Initialize a variable to store the carry during addition.
10 : Node Initialization
ListNode* node = NULL, *prv = NULL;
Initialize pointers for the result linked list: node for the current node and prv for the previous node.
11 : Addition Loop
while(!s1.empty() || !s2.empty()) {
Start the loop to add corresponding digits from the two stacks while there are still elements in either stack.
12 : Addition from S1
if(!s1.empty()) {
Check if stack s1 is not empty and add its top element to the carry.
13 : Update Carry
cry += s1.top();
Add the top element of stack s1 to the carry.
14 : Pop Operation
s1.pop();
Pop the top element of stack s1.
15 : Addition from S2
if(!s2.empty()) {
Check if stack s2 is not empty and add its top element to the carry.
16 : Update Carry
cry += s2.top();
Add the top element of stack s2 to the carry.
17 : Pop Operation
s2.pop();
Pop the top element of stack s2.
18 : Create Node
node = new ListNode(cry % 10);
Create a new node with the value of the current digit (cry % 10).
19 : Update Node Links
node->next = prv;
Link the newly created node to the previous node.
20 : Update Previous Node
prv = node;
Update the previous node pointer to the current node.
21 : Update Carry
cry /= 10;
Update the carry by dividing it by 10.
22 : Create Final Node
if(cry != 0) {
Check if there is a remaining carry to create a final node.
23 : Final Carry Node
node = new ListNode(cry %10);
Create a final node with the carry value.
24 : Update Node Links
node->next = prv;
Link the final node to the result list.
25 : Return Final Node
return node;
Return the head of the newly created linked list that represents the sum.
Best Case: O(n), where n is the length of the longer linked list.
Average Case: O(n), as each node is processed once.
Worst Case: O(n), as we process both lists in full.
Description: The time complexity is linear because we traverse each list once.
Best Case: O(n), since we still need stacks for the input digits.
Worst Case: O(n), due to the use of two stacks for storing the digits of both lists.
Description: The space complexity is linear because we use stacks to hold the digits before performing the addition.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus