-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp021.c
47 lines (47 loc) · 1018 Bytes
/
p021.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
struct ListNode *min, *max;
if (l1->val < l2->val)
{
min = l1;
max = l2;
}
else
{
min = l2;
max = l1;
}
struct ListNode *head = min;
while (max != NULL)
{
if (min->next == NULL)
{
min->next = max;
break;
}
else
{
if (max->val < min->next->val)
{
struct ListNode *maxnext = max->next;
max->next = min->next;
min->next = max;
min = max;
max = maxnext;
}
else
{
min = min->next;
}
}
}
return head;
}