File tree Expand file tree Collapse file tree 1 file changed +9
-12
lines changed
solution/0000-0099/0002.Add Two Numbers Expand file tree Collapse file tree 1 file changed +9
-12
lines changed Original file line number Diff line number Diff line change @@ -493,6 +493,7 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
493
493
#### C
494
494
495
495
` ` ` c
496
+
496
497
/**
497
498
* Definition for singly-linked list.
498
499
* struct ListNode {
@@ -501,39 +502,35 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
501
502
* };
502
503
*/
503
504
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));
507
507
dummy- > val = 0 ;
508
508
dummy- > next = NULL ;
509
- struct ListNode * curr = dummy;
509
+ struct ListNode* curr = dummy;
510
510
int carry = 0 ;
511
511
512
- while (l1 != NULL || l2 != NULL || carry != 0 )
513
- {
512
+ while (l1 != NULL || l2 != NULL || carry != 0 ) {
514
513
int sum = carry;
515
- if (l1 != NULL )
516
- {
514
+ if (l1 != NULL ) {
517
515
sum += l1- > val;
518
516
l1 = l1- > next;
519
517
}
520
- if (l2 != NULL )
521
- {
518
+ if (l2 != NULL ) {
522
519
sum += l2- > val;
523
520
l2 = l2- > next;
524
521
}
525
522
526
523
carry = sum / 10 ;
527
524
int val = sum % 10 ;
528
525
529
- struct ListNode * newNode = (struct ListNode * ) malloc (sizeof (struct ListNode));
526
+ struct ListNode* newNode = (struct ListNode* ) malloc (sizeof (struct ListNode));
530
527
newNode- > val = val;
531
528
newNode- > next = NULL ;
532
529
curr- > next = newNode;
533
530
curr = curr- > next;
534
531
}
535
532
536
- struct ListNode * result = dummy- > next;
533
+ struct ListNode* result = dummy- > next;
537
534
free (dummy);
538
535
return result;
539
536
}
You can’t perform that action at this time.
0 commit comments