Skip to content

Commit 0a69e29

Browse files
Points Not showing in the leaderboard
1 parent 01b919d commit 0a69e29

File tree

1 file changed

+65
-67
lines changed

1 file changed

+65
-67
lines changed

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

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -250,75 +250,73 @@ function addTwoNumbersProblem() {
250250
int x = l1 ? l1->val : 0;
251251
int y
252252

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-
```
267-
</TabItem>
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>
268267
<TabItem value="C" label="C">
269268
<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>
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>
298296
<TabItem value="ts" label="TypeScript">
299297
<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>
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>
322320
</Tabs>
323321
324322
### Complexity Analysis
@@ -351,7 +349,7 @@ Output: [8,9,9,9,0,0,0,1]
351349

352350
:::info
353351

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))$.
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))$.
355353

356354
:::
357355

@@ -363,4 +361,4 @@ Output: [8,9,9,9,0,0,0,1]
363361
{['ajay-dhangar'].map(username => (
364362
<Author key={username} username={username} />
365363
))}
366-
</div>
364+
</div>

0 commit comments

Comments
 (0)