From 0a69e29ae692e241d6827569f2e9eb3e5612a808 Mon Sep 17 00:00:00 2001
From: Sadaf <137484958+SadafKausar2025@users.noreply.github.com>
Date: Tue, 30 Jul 2024 09:39:27 +0530
Subject: [PATCH 1/2] Points Not showing in the leaderboard
---
.../0000-0099/0002-Add-Two-Numbers.md | 132 +++++++++---------
1 file changed, 65 insertions(+), 67 deletions(-)
diff --git a/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md b/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
index 1d1aa8d06..7ca7017c8 100644
--- a/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
+++ b/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
@@ -250,75 +250,73 @@ function addTwoNumbersProblem() {
int x = l1 ? l1->val : 0;
int y
- = l2 ? l2->val : 0;
- int sum = x + y + carry;
- carry = sum / 10;
- curr->next = new ListNode(sum % 10);
- curr = curr->next;
- if (l1) l1 = l1->next;
- if (l2) l2 = l2->next;
- }
- if (carry > 0) {
- curr->next = new ListNode(carry);
- }
- return dummy->next;
- }
- ```
-
+= l2 ? l2->val : 0;
+int sum = x + y + carry;
+carry = sum / 10;
+curr->next = new ListNode(sum % 10);
+curr = curr->next;
+if (l1) l1 = l1->next;
+if (l2) l2 = l2->next;
+}
+if (carry > 0) {
+curr->next = new ListNode(carry);
+}
+return dummy->next;
+}
+`
- ```c
- struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
- struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
- dummy->val = 0;
- dummy->next = NULL;
- struct ListNode* curr = dummy;
- int carry = 0;
- while (l1 || l2) {
- int x = l1 ? l1->val : 0;
- int y = l2 ? l2->val : 0;
- int sum = x + y + carry;
- carry = sum / 10;
- curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
- curr->next->val = sum % 10;
- curr->next->next = NULL;
- curr = curr->next;
- if (l1) l1 = l1->next;
- if (l2) l2 = l2->next;
- }
- if (carry > 0) {
- curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
- curr->next->val = carry;
- curr->next->next = NULL;
- }
- return dummy->next;
- }
- ```
-
+ `c
+struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
+struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
+dummy->val = 0;
+dummy->next = NULL;
+struct ListNode* curr = dummy;
+int carry = 0;
+while (l1 || l2) {
+int x = l1 ? l1->val : 0;
+int y = l2 ? l2->val : 0;
+int sum = x + y + carry;
+carry = sum / 10;
+curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
+curr->next->val = sum % 10;
+curr->next->next = NULL;
+curr = curr->next;
+if (l1) l1 = l1->next;
+if (l2) l2 = l2->next;
+}
+if (carry > 0) {
+curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
+curr->next->val = carry;
+curr->next->next = NULL;
+}
+return dummy->next;
+}
+`
- ```typescript
- function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
- let dummy = new ListNode(0);
- let curr = dummy;
- let carry = 0;
- while (l1 || l2) {
- let x = l1 ? l1.val : 0;
- let y = l2 ? l2.val : 0;
- let sum = x + y + carry;
- carry = Math.floor(sum / 10);
- curr.next = new ListNode(sum % 10);
- curr = curr.next;
- if (l1) l1 = l1.next;
- if (l2) l2 = l2.next;
- }
- if (carry > 0) {
- curr.next = new ListNode(carry);
- }
- return dummy.next;
- }
- ```
-
+ `typescript
+function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
+let dummy = new ListNode(0);
+let curr = dummy;
+let carry = 0;
+while (l1 || l2) {
+let x = l1 ? l1.val : 0;
+let y = l2 ? l2.val : 0;
+let sum = x + y + carry;
+carry = Math.floor(sum / 10);
+curr.next = new ListNode(sum % 10);
+curr = curr.next;
+if (l1) l1 = l1.next;
+if (l2) l2 = l2.next;
+}
+if (carry > 0) {
+curr.next = new ListNode(carry);
+}
+return dummy.next;
+}
+```
+
### Complexity Analysis
@@ -351,7 +349,7 @@ Output: [8,9,9,9,0,0,0,1]
:::info
-**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))$.
+**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))$.
:::
@@ -363,4 +361,4 @@ Output: [8,9,9,9,0,0,0,1]
{['ajay-dhangar'].map(username => (
))}
-
\ No newline at end of file
+
From 16f73c323301a49e8bb6b95219985bf903786de3 Mon Sep 17 00:00:00 2001
From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com>
Date: Tue, 30 Jul 2024 11:55:01 +0530
Subject: [PATCH 2/2] Update 0002-Add-Two-Numbers.md
---
.../0000-0099/0002-Add-Two-Numbers.md | 144 +++++-------------
1 file changed, 39 insertions(+), 105 deletions(-)
diff --git a/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md b/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
index 7ca7017c8..6b52419e7 100644
--- a/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
+++ b/dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
@@ -190,10 +190,10 @@ function addTwoNumbersProblem() {
return dummy.next;
};
```
-
+
-
-
+
+
```python
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
@@ -214,9 +214,10 @@ function addTwoNumbersProblem() {
curr.next = ListNode(carry)
return dummy.next
```
-
-
-
+
+
+
+
```java
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
@@ -238,120 +239,53 @@ function addTwoNumbersProblem() {
return dummy.next;
}
```
-
-
-
+
+
+
+
```cpp
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* curr = dummy;
int carry = 0;
while (l1 || l2) {
- int x = l1 ? l1->val : 0;
- int y
-
-= l2 ? l2->val : 0;
-int sum = x + y + carry;
-carry = sum / 10;
-curr->next = new ListNode(sum % 10);
-curr = curr->next;
-if (l1) l1 = l1->next;
-if (l2) l2 = l2->next;
-}
-if (carry > 0) {
-curr->next = new ListNode(carry);
-}
-return dummy->next;
-}
-`
-
-
- `c
-struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
-struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
-dummy->val = 0;
-dummy->next = NULL;
-struct ListNode* curr = dummy;
-int carry = 0;
-while (l1 || l2) {
-int x = l1 ? l1->val : 0;
-int y = l2 ? l2->val : 0;
-int sum = x + y + carry;
-carry = sum / 10;
-curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
-curr->next->val = sum % 10;
-curr->next->next = NULL;
-curr = curr->next;
-if (l1) l1 = l1->next;
-if (l2) l2 = l2->next;
-}
-if (carry > 0) {
-curr->next = (struct ListNode*)malloc(sizeof(struct ListNode));
-curr->next->val = carry;
-curr->next->next = NULL;
-}
-return dummy->next;
-}
-`
-
-
- `typescript
-function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
-let dummy = new ListNode(0);
-let curr = dummy;
-let carry = 0;
-while (l1 || l2) {
-let x = l1 ? l1.val : 0;
-let y = l2 ? l2.val : 0;
-let sum = x + y + carry;
-carry = Math.floor(sum / 10);
-curr.next = new ListNode(sum % 10);
-curr = curr.next;
-if (l1) l1 = l1.next;
-if (l2) l2 = l2.next;
-}
-if (carry > 0) {
-curr.next = new ListNode(carry);
-}
-return dummy.next;
-}
-```
-
+ int x = (l1) ? l1->val : 0;
+ int y = (l2) ? l2->val : 0;
+ int sum = x + y + carry;
+ carry = sum / 10;
+ curr->next = new ListNode(sum % 10);
+ curr = curr->next;
+ if (l1) l1 = l1->next;
+ if (l2) l2 = l2->next;
+ }
+ if (carry > 0) {
+ curr->next = new ListNode(carry);
+ }
+ return dummy->next;
+ }
+ ```
+
-### Complexity Analysis
+## Complexity Analysis
-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.
+### Time Complexity
+The time complexity of the solution is **O(n)**, where **n** is the maximum length of the input linked lists.
-### Test Cases
+### Space Complexity
+The space complexity is **O(n)** due to the space required to store the resulting linked list.
-
-
-```plaintext
-Input: l1 = [2,4,3], l2 = [5,6,4]
-Output: [7,0,8]
-```
-
-
-```plaintext
-Input: l1 = [0], l2 = [0]
-Output: [0]
-```
-
+## Conclusion
-
-```plaintext
-Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
-Output: [8,9,9,9,0,0,0,1]
-```
-
-
+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.
-:::info
+---
-**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))$.
+## References
-:::
+- [LeetCode Problem](https://leetcode.com/problems/add-two-numbers/)
+- [GeeksforGeeks Solution](https://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists/)
+- [YouTube Explanation](https://www.youtube.com/watch?v=wgFPrzTjm7s)
---