Skip to content

Commit 277bd76

Browse files
authored
Merge pull request #4082 from SadafKausar2025/demo
Points Not showing in the leaderboard
2 parents a25fb7d + 16f73c3 commit 277bd76

File tree

1 file changed

+27
-95
lines changed

1 file changed

+27
-95
lines changed

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

+27-95
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,19 +239,18 @@ 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;
252+
int x = (l1) ? l1->val : 0;
253+
int y = (l2) ? l2->val : 0;
254254
int sum = x + y + carry;
255255
carry = sum / 10;
256256
curr->next = new ListNode(sum % 10);
@@ -264,96 +264,28 @@ function addTwoNumbersProblem() {
264264
return dummy->next;
265265
}
266266
```
267-
</TabItem>
268-
<TabItem value="C" label="C">
269-
<SolutionAuthor name="@ajay-dhangar"/>
270-
```c
271-
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
272-
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
273-
dummy->val = 0;
274-
dummy->next = NULL;
275-
struct ListNode* curr = dummy;
276-
int carry = 0;
277-
while (l1 || l2) {
278-
int x = l1 ? l1->val : 0;
279-
int y = l2 ? l2->val : 0;
280-
int sum = x + y + carry;
281-
carry = sum / 10;
282-
curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
283-
curr->next->val = sum % 10;
284-
curr->next->next = NULL;
285-
curr = curr->next;
286-
if (l1) l1 = l1->next;
287-
if (l2) l2 = l2->next;
288-
}
289-
if (carry > 0) {
290-
curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
291-
curr->next->val = carry;
292-
curr->next->next = NULL;
293-
}
294-
return dummy->next;
295-
}
296-
```
297-
</TabItem>
298-
<TabItem value="ts" label="TypeScript">
299-
<SolutionAuthor name="@ajay-dhangar"/>
300-
```typescript
301-
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
302-
let dummy = new ListNode(0);
303-
let curr = dummy;
304-
let carry = 0;
305-
while (l1 || l2) {
306-
let x = l1 ? l1.val : 0;
307-
let y = l2 ? l2.val : 0;
308-
let sum = x + y + carry;
309-
carry = Math.floor(sum / 10);
310-
curr.next = new ListNode(sum % 10);
311-
curr = curr.next;
312-
if (l1) l1 = l1.next;
313-
if (l2) l2 = l2.next;
314-
}
315-
if (carry > 0) {
316-
curr.next = new ListNode(carry);
317-
}
318-
return dummy.next;
319-
}
320-
```
321-
</TabItem>
267+
</TabItem>
322268
</Tabs>
323269

324-
### Complexity Analysis
270+
## Complexity Analysis
325271

326-
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.
327274

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

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

344-
<TabItem value="TestCase3" label="Case 3">
345-
```plaintext
346-
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
347-
Output: [8,9,9,9,0,0,0,1]
348-
```
349-
</TabItem>
350-
</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.
351281

352-
:::info
282+
---
353283

354-
**Note:** The above code is a solution to the Add Two Numbers problem on LeetCode. It is a simple and efficient solution that uses a dummy node to keep 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
355285

356-
:::
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)
357289

358290
---
359291

@@ -363,4 +295,4 @@ Output: [8,9,9,9,0,0,0,1]
363295
{['ajay-dhangar'].map(username => (
364296
<Author key={username} username={username} />
365297
))}
366-
</div>
298+
</div>

0 commit comments

Comments
 (0)