Skip to content

Commit 16f73c3

Browse files
authored
Update 0002-Add-Two-Numbers.md
1 parent 0a69e29 commit 16f73c3

File tree

1 file changed

+39
-105
lines changed

1 file changed

+39
-105
lines changed

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

+39-105
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ function addTwoNumbersProblem() {
190190
return dummy.next;
191191
};
192192
```
193-
</TabItem>
193+
</TabItem>
194194

195-
<TabItem value="Python" label="Python">
196-
<SolutionAuthor name="@amruta-jayanti"/>
195+
<TabItem value="Python" label="Python">
196+
<SolutionAuthor name="@ajay-dhangar"/>
197197
```python
198198
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
199199
dummy = ListNode(0)
@@ -214,9 +214,10 @@ function addTwoNumbersProblem() {
214214
curr.next = ListNode(carry)
215215
return dummy.next
216216
```
217-
</TabItem>
218-
<TabItem value="Java" label="Java">
219-
<SolutionAuthor name="@amruta-jayanti"/>
217+
</TabItem>
218+
219+
<TabItem value="Java" label="Java">
220+
<SolutionAuthor name="@ajay-dhangar"/>
220221
```java
221222
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
222223
ListNode dummy = new ListNode(0);
@@ -238,120 +239,53 @@ function addTwoNumbersProblem() {
238239
return dummy.next;
239240
}
240241
```
241-
</TabItem>
242-
<TabItem value="C++" label="C++">
243-
<SolutionAuthor name="@amruta-jayanti"/>
242+
</TabItem>
243+
244+
<TabItem value="C++" label="C++">
245+
<SolutionAuthor name="@ajay-dhangar"/>
244246
```cpp
245247
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
246248
ListNode* dummy = new ListNode(0);
247249
ListNode* curr = dummy;
248250
int carry = 0;
249251
while (l1 || l2) {
250-
int x = l1 ? l1->val : 0;
251-
int y
252-
253-
= l2 ? l2->val : 0;
254-
int sum = x + y + carry;
255-
carry = sum / 10;
256-
curr->next = new ListNode(sum % 10);
257-
curr = curr->next;
258-
if (l1) l1 = l1->next;
259-
if (l2) l2 = l2->next;
260-
}
261-
if (carry > 0) {
262-
curr->next = new ListNode(carry);
263-
}
264-
return dummy->next;
265-
}
266-
` </TabItem>
267-
<TabItem value="C" label="C">
268-
<SolutionAuthor name="@ajay-dhangar"/>
269-
`c
270-
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
271-
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
272-
dummy->val = 0;
273-
dummy->next = NULL;
274-
struct ListNode* curr = dummy;
275-
int carry = 0;
276-
while (l1 || l2) {
277-
int x = l1 ? l1->val : 0;
278-
int y = l2 ? l2->val : 0;
279-
int sum = x + y + carry;
280-
carry = sum / 10;
281-
curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
282-
curr->next->val = sum % 10;
283-
curr->next->next = NULL;
284-
curr = curr->next;
285-
if (l1) l1 = l1->next;
286-
if (l2) l2 = l2->next;
287-
}
288-
if (carry > 0) {
289-
curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
290-
curr->next->val = carry;
291-
curr->next->next = NULL;
292-
}
293-
return dummy->next;
294-
}
295-
` </TabItem>
296-
<TabItem value="ts" label="TypeScript">
297-
<SolutionAuthor name="@ajay-dhangar"/>
298-
`typescript
299-
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
300-
let dummy = new ListNode(0);
301-
let curr = dummy;
302-
let carry = 0;
303-
while (l1 || l2) {
304-
let x = l1 ? l1.val : 0;
305-
let y = l2 ? l2.val : 0;
306-
let sum = x + y + carry;
307-
carry = Math.floor(sum / 10);
308-
curr.next = new ListNode(sum % 10);
309-
curr = curr.next;
310-
if (l1) l1 = l1.next;
311-
if (l2) l2 = l2.next;
312-
}
313-
if (carry > 0) {
314-
curr.next = new ListNode(carry);
315-
}
316-
return dummy.next;
317-
}
318-
```
319-
</TabItem>
252+
int x = (l1) ? l1->val : 0;
253+
int y = (l2) ? l2->val : 0;
254+
int sum = x + y + carry;
255+
carry = sum / 10;
256+
curr->next = new ListNode(sum % 10);
257+
curr = curr->next;
258+
if (l1) l1 = l1->next;
259+
if (l2) l2 = l2->next;
260+
}
261+
if (carry > 0) {
262+
curr->next = new ListNode(carry);
263+
}
264+
return dummy->next;
265+
}
266+
```
267+
</TabItem>
320268
</Tabs>
321269

322-
### Complexity Analysis
270+
## Complexity Analysis
323271

324-
The time complexity for this solution is $O(\max(m, n))$, where m and n are the lengths of the two linked lists. We iterate through both linked lists once, and the space complexity is $O(\max(m, n))$, where m and n are the lengths of the two linked lists. The space complexity is due to the new linked list created to store the result.
272+
### Time Complexity
273+
The time complexity of the solution is **O(n)**, where **n** is the maximum length of the input linked lists.
325274

326-
### Test Cases
275+
### Space Complexity
276+
The space complexity is **O(n)** due to the space required to store the resulting linked list.
327277

328-
<Tabs>
329-
<TabItem value="TestCase1" label="Case 1">
330-
```plaintext
331-
Input: l1 = [2,4,3], l2 = [5,6,4]
332-
Output: [7,0,8]
333-
```
334-
</TabItem>
335-
<TabItem value="TestCase2" label="Case 2">
336-
```plaintext
337-
Input: l1 = [0], l2 = [0]
338-
Output: [0]
339-
```
340-
</TabItem>
278+
## Conclusion
341279

342-
<TabItem value="TestCase3" label="Case 3">
343-
```plaintext
344-
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
345-
Output: [8,9,9,9,0,0,0,1]
346-
```
347-
</TabItem>
348-
</Tabs>
280+
The solution provided efficiently adds two numbers represented by linked lists. By using dummy nodes and handling carry, we ensure that the solution is both easy to understand and efficient. The detailed explanation, flowchart, and code in multiple languages aim to help you understand and implement the solution effectively.
349281

350-
:::info
282+
---
351283

352-
**Note:** The above code is a solution of the Add Two Numbers problem on LeetCode. It is a simple and efficient solution that uses a dummy node to keep a track of the result linked list. The solution iterates through both linked lists, adding the corresponding node values and carry to generate the result. The time complexity of this solution is $O(\max(m, n))$, where m and n are the lengths of the two linked lists, and the space complexity is $O(\max(m, n))$.
284+
## References
353285

354-
:::
286+
- [LeetCode Problem](https://leetcode.com/problems/add-two-numbers/)
287+
- [GeeksforGeeks Solution](https://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists/)
288+
- [YouTube Explanation](https://www.youtube.com/watch?v=wgFPrzTjm7s)
355289

356290
---
357291

0 commit comments

Comments
 (0)