-
Notifications
You must be signed in to change notification settings - Fork 31
/
0002-add-two-numbers.cpp
58 lines (46 loc) · 2.14 KB
/
0002-add-two-numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Problem: LeetCode 2 - Add Two Numbers
Description:
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.
Intuition:
We can traverse the two linked lists simultaneously, adding the digits at the same position and keeping track of the carry. As we move forward, we create a new node for the sum and update the carry for the next iteration. If any linked list has more digits left, we continue the process until both lists are fully traversed.
Approach:
1. Initialize a dummy node to the head of the result list.
2. Initialize variables `carry` and `sum` to 0.
3. Traverse both linked lists simultaneously until both are fully traversed.
4. At each step, compute the sum of digits and the carry for the next iteration.
5. Create a new node with the sum and attach it to the result list.
6. Move the current pointers of both input lists to the next nodes.
7. If any list has remaining digits, continue adding them to the result.
8. Return the head of the result list after skipping the dummy node.
Time Complexity:
The time complexity is O(max(N, M)), where N and M are the number of nodes in the input linked lists. We traverse both lists once.
Space Complexity:
The space complexity is O(max(N, M)), where N and M are the number of nodes in the input linked lists. The extra space is used to store the result list.
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *dummy = new ListNode(0);
ListNode *current = dummy;
int carry = 0;
while (l1 || l2) {
int sum = carry;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}
if (carry) {
current->next = new ListNode(carry);
}
return dummy->next;
}
};