Skip to content

Commit 3ec8217

Browse files
committed
git commit " solution of 0002
0000-0100 0002 Solution of this in c a=language"
1 parent 276efed commit 3ec8217

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,57 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
494494
result = aggregate
495495
```
496496
497+
#### C
498+
499+
```c
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+
```
546+
547+
497548
<!-- tabs:end -->
498549
499550
<!-- solution:end -->

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

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