Skip to content

Commit 9de9aba

Browse files
committed
olution and Readme file updated with all the terms
1 parent 6085f2f commit 9de9aba

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

solution/0000-0099/0002.Add Two Numbers/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,55 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
494494
result = aggregate
495495
```
496496
497+
#### C
498+
``` c
499+
500+
/**
501+
* Definition for singly-linked list.
502+
* struct ListNode {
503+
* int val;
504+
* struct ListNode *next;
505+
* };
506+
*/
507+
508+
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
509+
{
510+
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
511+
dummy->val = 0;
512+
dummy->next = NULL;
513+
struct ListNode *curr = dummy;
514+
int carry = 0;
515+
516+
while (l1 != NULL || l2 != NULL || carry != 0)
517+
{
518+
int sum = carry;
519+
if (l1 != NULL)
520+
{
521+
sum += l1->val;
522+
l1 = l1->next;
523+
}
524+
if (l2 != NULL)
525+
{
526+
sum += l2->val;
527+
l2 = l2->next;
528+
}
529+
530+
carry = sum / 10;
531+
int val = sum % 10;
532+
533+
struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
534+
newNode->val = val;
535+
newNode->next = NULL;
536+
curr->next = newNode;
537+
curr = curr->next;
538+
}
539+
540+
struct ListNode *result = dummy->next;
541+
free(dummy);
542+
return result;
543+
}
544+
```
545+
497546
<!-- tabs:end -->
498547
499548
<!-- solution:end -->

solution/0000-0099/0002.Add Two Numbers/README_EN.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,54 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
490490
result = aggregate
491491
```
492492
493+
#### C
494+
```c
495+
/**
496+
* Definition for singly-linked list.
497+
* struct ListNode {
498+
* int val;
499+
* struct ListNode *next;
500+
* };
501+
*/
502+
503+
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
504+
{
505+
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
506+
dummy->val = 0;
507+
dummy->next = NULL;
508+
struct ListNode *curr = dummy;
509+
int carry = 0;
510+
511+
while (l1 != NULL || l2 != NULL || carry != 0)
512+
{
513+
int sum = carry;
514+
if (l1 != NULL)
515+
{
516+
sum += l1->val;
517+
l1 = l1->next;
518+
}
519+
if (l2 != NULL)
520+
{
521+
sum += l2->val;
522+
l2 = l2->next;
523+
}
524+
525+
carry = sum / 10;
526+
int val = sum % 10;
527+
528+
struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
529+
newNode->val = val;
530+
newNode->next = NULL;
531+
curr->next = newNode;
532+
curr = curr->next;
533+
}
534+
535+
struct ListNode *result = dummy->next;
536+
free(dummy);
537+
return result;
538+
}
539+
```
540+
493541
<!-- tabs:end -->
494542
495543
<!-- solution:end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
9+
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
10+
struct ListNode* dummy = (struct ListNode*) malloc(sizeof(struct ListNode));
11+
dummy->val = 0;
12+
dummy->next = NULL;
13+
struct ListNode* curr = dummy;
14+
int carry = 0;
15+
16+
while (l1 != NULL || l2 != NULL || carry != 0) {
17+
int sum = carry;
18+
if (l1 != NULL) {
19+
sum += l1->val;
20+
l1 = l1->next;
21+
}
22+
if (l2 != NULL) {
23+
sum += l2->val;
24+
l2 = l2->next;
25+
}
26+
27+
carry = sum / 10;
28+
int val = sum % 10;
29+
30+
struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode));
31+
newNode->val = val;
32+
newNode->next = NULL;
33+
curr->next = newNode;
34+
curr = curr->next;
35+
}
36+
37+
struct ListNode* result = dummy->next;
38+
free(dummy);
39+
return result;
40+
}

0 commit comments

Comments
 (0)