-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsrc.c
64 lines (62 loc) · 1.11 KB
/
src.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2) {
struct ListNode *p = l1, *q = l2, *result = NULL, *k = NULL;
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1;
if (p -> val <= q->val) {
result = p;
p = p->next;
} else {
result = q;
q = q->next;
}
k = result;
while (p && q) {
if (p->val <= q->val) {
k->next = p;
p = p->next;
} else {
k->next = q;
q = q->next;
}
k = k->next;
}
while (p) {
k->next = p;
p = p->next;
k = k->next;
}
while (q) {
k->next = q;
q = q->next;
k = k->next;
}
return result;
}
int main(int argc, char **argv)
{
struct ListNode *l1 = NULL, *l2 = NULL;
l1 = malloc(sizeof(*l1));
l1->val = 2;
l1->next = malloc(sizeof(*l1));
l1->next->val = 4;
l1->next->next = NULL;
l2 = malloc(sizeof(*l2));
l2->val = 1;
l2->next = malloc(sizeof(*l2));
l2->next->val = 3;
l2->next->next = NULL;
struct ListNode *result = mergeTwoLists(l1, l2);
while(result) {
printf("%d ", result->val);
result = result->next;
}
return 0;
}