Skip to content

Commit 55bc9c9

Browse files
committed
2 parents c05b7a5 + bb9981e commit 55bc9c9

File tree

3 files changed

+49
-55
lines changed

3 files changed

+49
-55
lines changed

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

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -499,48 +499,44 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
499499
```c
500500

501501
/**
502-
* Definition for singly-linked list.
503-
* struct ListNode {
504-
* int val;
505-
* struct ListNode *next;
506-
* };
507-
*/
508-
509-
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
510-
{
511-
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
512-
dummy->val = 0;
513-
dummy->next = NULL;
514-
struct ListNode *curr = dummy;
515-
int carry = 0;
516-
517-
while (l1 != NULL || l2 != NULL || carry != 0)
518-
{
519-
int sum = carry;
520-
if (l1 != NULL)
521-
{
522-
sum += l1->val;
523-
l1 = l1->next;
524-
}
525-
if (l2 != NULL)
526-
{
527-
sum += l2->val;
528-
l2 = l2->next;
529-
}
530-
531-
carry = sum / 10;
532-
int val = sum % 10;
533-
534-
struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
535-
newNode->val = val;
536-
newNode->next = NULL;
537-
curr->next = newNode;
538-
curr = curr->next;
539-
}
540-
541-
struct ListNode *result = dummy->next;
542-
free(dummy);
543-
return result;
502+
* Definition for singly-linked list.
503+
* struct ListNode {
504+
* int val;
505+
* struct ListNode *next;
506+
* };
507+
*/
508+
509+
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
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+
int sum = carry;
518+
if (l1 != NULL) {
519+
sum += l1->val;
520+
l1 = l1->next;
521+
}
522+
if (l2 != NULL) {
523+
sum += l2->val;
524+
l2 = l2->next;
525+
}
526+
527+
carry = sum / 10;
528+
int val = sum % 10;
529+
530+
struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode));
531+
newNode->val = val;
532+
newNode->next = NULL;
533+
curr->next = newNode;
534+
curr = curr->next;
535+
}
536+
537+
struct ListNode* result = dummy->next;
538+
free(dummy);
539+
return result;
544540
}
545541
```
546542

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
493493
#### C
494494
495495
```c
496+
496497
/**
497498
* Definition for singly-linked list.
498499
* struct ListNode {
@@ -501,39 +502,35 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
501502
* };
502503
*/
503504

504-
struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
505-
{
506-
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
505+
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
506+
struct ListNode* dummy = (struct ListNode*) malloc(sizeof(struct ListNode));
507507
dummy->val = 0;
508508
dummy->next = NULL;
509-
struct ListNode *curr = dummy;
509+
struct ListNode* curr = dummy;
510510
int carry = 0;
511511

512-
while (l1 != NULL || l2 != NULL || carry != 0)
513-
{
512+
while (l1 != NULL || l2 != NULL || carry != 0) {
514513
int sum = carry;
515-
if (l1 != NULL)
516-
{
514+
if (l1 != NULL) {
517515
sum += l1->val;
518516
l1 = l1->next;
519517
}
520-
if (l2 != NULL)
521-
{
518+
if (l2 != NULL) {
522519
sum += l2->val;
523520
l2 = l2->next;
524521
}
525522

526523
carry = sum / 10;
527524
int val = sum % 10;
528525

529-
struct ListNode *newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
526+
struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode));
530527
newNode->val = val;
531528
newNode->next = NULL;
532529
curr->next = newNode;
533530
curr = curr->next;
534531
}
535532

536-
struct ListNode *result = dummy->next;
533+
struct ListNode* result = dummy->next;
537534
free(dummy);
538535
return result;
539536
}

solution/0000-0099/0002.Add Two Numbers/Solution.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/**
23
* Definition for singly-linked list.
34
* struct ListNode {
@@ -37,4 +38,4 @@ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
3738
struct ListNode* result = dummy->next;
3839
free(dummy);
3940
return result;
40-
}
41+
}

0 commit comments

Comments
 (0)