Skip to content

Commit 515fa64

Browse files
committed
The LeetCode problem [21]'s solution.
1 parent 7fb88e4 commit 515fa64

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/21.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* struct ListNode {
6+
* int val;
7+
* struct ListNode *next;
8+
* };
9+
*/
10+
struct ListNode {
11+
int val;
12+
struct ListNode *next;
13+
};
14+
15+
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
16+
struct ListNode *retList = NULL, *indexList = NULL;
17+
struct ListNode *first = l1, *two = l2;
18+
19+
if(first == NULL) {
20+
return two;
21+
} else if(two == NULL) {
22+
return first;
23+
} else if(first->val > two->val) {
24+
retList = two;
25+
two = two->next;
26+
27+
retList->next = NULL;
28+
indexList = retList;
29+
} else {
30+
retList = first;
31+
first = first->next;
32+
33+
retList->next = NULL;
34+
indexList = retList;
35+
}
36+
37+
while(first || two) {
38+
if(first == NULL) {
39+
indexList->next = two;
40+
two = two->next;
41+
42+
indexList = indexList->next;
43+
indexList->next = NULL;
44+
} else if(two == NULL) {
45+
indexList->next = first;
46+
first = first->next;
47+
48+
indexList = indexList->next;
49+
indexList->next = NULL;
50+
} else if(first->val > two->val) {
51+
indexList->next = two;
52+
two = two->next;
53+
54+
indexList = indexList->next;
55+
indexList->next = NULL;
56+
} else {
57+
indexList->next = first;
58+
first = first->next;
59+
60+
indexList = indexList->next;
61+
indexList->next = NULL;
62+
}
63+
}
64+
65+
return retList;
66+
}
67+
68+
int main()
69+
{
70+
return 0;
71+
}

0 commit comments

Comments
 (0)