Skip to content

Commit 80e768c

Browse files
Update 0002-Add-Two-Numbers.md
1 parent 8210ef3 commit 80e768c

File tree

1 file changed

+87
-11
lines changed

1 file changed

+87
-11
lines changed

dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,46 @@ description: "This is a solution to the Add Two Numbers problem on LeetCode."
1010
---
1111
## Problem Description
1212

13-
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.
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------- |
15+
| [Add Two Numbers on LeetCode](https://leetcode.com/problems/add-two-numbers/) | [Add Two Numbers Solution on LeetCode](https://leetcode.com/problems/add-two-numbers/solutions/5234194/solution/) | [Amruta Jayanti](https://leetcode.com/u/user7669cY/)|
16+
17+
18+
## Problem Description
19+
20+
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.
1421

1522
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
1623

1724

1825

19-
Example 1:
20-
26+
**Example 1:**
2127

28+
```plaintext
2229
Input: l1 = [2,4,3], l2 = [5,6,4]
2330
Output: [7,0,8]
2431
Explanation: 342 + 465 = 807.
25-
Example 2:
32+
```
33+
34+
**Example 2:**
2635

36+
```plaintext
2737
Input: l1 = [0], l2 = [0]
2838
Output: [0]
29-
Example 3:
39+
```
40+
41+
**Example 3:**
3042

43+
```plaintext
3144
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
3245
Output: [8,9,9,9,0,0,0,1]
33-
46+
```
3447

35-
Constraints:
48+
### Constraints:
3649

37-
The number of nodes in each linked list is in the range [1, 100].
38-
0 <= Node.val <= 9
39-
It is guaranteed that the list represents a number that does not have leading zeros.
50+
- The number of nodes in each linked list is in the range `[1, 100]`.
51+
- `0 <= Node.val <= 9`
52+
- It is guaranteed that the list represents a number that does not have leading zeros.
4053

4154

4255
## Solution to the problem
@@ -86,9 +99,72 @@ class Solution(object):
8699
Above is the implementation in Python. Here total_sum stores the value and adds to the dummy. Variable carry is used to handle the carry bits.
87100

88101
#### Complexity Analysis:
89-
- Time Complexity : $$O(max(n,m))$$ .Here, n,m are the lengths of the input linked lists
102+
- Time Complexity : $$O(max(n,m))$$ Here, n,m are the lengths of the input linked lists
90103
- Space Complexity : $$O(max(n,m))$$
91104

105+
#### Codes in different languages:
106+
`CPP`:
107+
```cpp
108+
class Solution {
109+
public:
110+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
111+
ListNode* dummyHead = new ListNode(0);
112+
ListNode* tail = dummyHead;
113+
int carry = 0;
114+
115+
while (l1 != nullptr || l2 != nullptr || carry != 0) {
116+
int digit1 = (l1 != nullptr) ? l1->val : 0;
117+
int digit2 = (l2 != nullptr) ? l2->val : 0;
118+
119+
int sum = digit1 + digit2 + carry;
120+
int digit = sum % 10;
121+
carry = sum / 10;
122+
123+
ListNode* newNode = new ListNode(digit);
124+
tail->next = newNode;
125+
tail = tail->next;
126+
127+
l1 = (l1 != nullptr) ? l1->next : nullptr;
128+
l2 = (l2 != nullptr) ? l2->next : nullptr;
129+
}
130+
131+
ListNode* result = dummyHead->next;
132+
delete dummyHead;
133+
return result;
134+
}
135+
};
136+
```
137+
138+
`Java`:
139+
```java
140+
class Solution {
141+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
142+
ListNode dummyHead = new ListNode(0);
143+
ListNode tail = dummyHead;
144+
int carry = 0;
145+
146+
while (l1 != null || l2 != null || carry != 0) {
147+
int digit1 = (l1 != null) ? l1.val : 0;
148+
int digit2 = (l2 != null) ? l2.val : 0;
149+
150+
int sum = digit1 + digit2 + carry;
151+
int digit = sum % 10;
152+
carry = sum / 10;
153+
154+
ListNode newNode = new ListNode(digit);
155+
tail.next = newNode;
156+
tail = tail.next;
157+
158+
l1 = (l1 != null) ? l1.next : null;
159+
l2 = (l2 != null) ? l2.next : null;
160+
}
161+
162+
ListNode result = dummyHead.next;
163+
dummyHead.next = null;
164+
return result;
165+
}
166+
}
167+
```
92168

93169

94170

0 commit comments

Comments
 (0)