Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit853735d

Browse files
feat: add c solution to lc problem: No.0002 (#4426)
1 parent6085f2f commit853735d

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,52 @@ proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLi
494494
result= aggregate
495495
```
496496
497+
#### C
498+
499+
```c
500+
501+
/**
502+
* Definition for singly-linked list.
503+
* struct ListNode {
504+
* int val;
505+
* struct ListNode *next;
506+
* };
507+
*/
508+
509+
struct ListNode*addTwoNumbers(structListNode*l1,structListNode*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;
540+
}
541+
```
542+
497543
<!-- tabs:end -->
498544
499545
<!-- solution:end -->

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp