Skip to content

Commit 4e9aab5

Browse files
authored
Update README.md
1 parent 2e3bd2f commit 4e9aab5

File tree

1 file changed

+38
-42
lines changed
  • solution/0000-0099/0002.Add Two Numbers

1 file changed

+38
-42
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

0 commit comments

Comments
 (0)