-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_list
87 lines (82 loc) · 1.89 KB
/
sort_list
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Sort a linked list in O(n log n) time using constant space complexity.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//function mergeLists is used to merge two linked lists and return the head pointer.
ListNode *mergeLists(ListNode *h1, ListNode *h2)
{
ListNode *head;
if (!h1) return h2;
if (!h2) return h1;
if (h1->val<=h2->val)
{
head = h1;
h1=h1->next;
}
else
{
head = h2;
h2=h2->next;
}
ListNode *p = head;
while (h1&&h2)
{
if (h1->val<=h2->val)
{
p->next=h1;
p=h1;
h1=h1->next;
}
else
{
p->next=h2;
p=h2;
h2=h2->next;
}
}
if (!h1) p->next=h2;
if (!h2) p->next=h1;
return head;
}
ListNode *sortList(ListNode *head) {
if (!head ||! (head->next)) return head;
ListNode *slowptr=head, *fastptr=head->next;
while (fastptr && (fastptr->next))
{
slowptr=slowptr->next;
fastptr=fastptr->next->next;
}
ListNode *head1=slowptr->next;//the second pointer that points to the second linked list.
slowptr->next=NULL;
head= sortList(head);
head1 = sortList(head1);
return mergeLists(head,head1);
}
};
remark:
1. IDEA: Because we need to sort in O(n log n) time using constant space complexity, so we use divide&conquer.
2. A better version of mergeLists(ListNode *h1, ListNode *h2) using recursion.
ListNode *mergeLists(ListNode *h1, ListNode *h2)
{
ListNode *head;
if (!h1) return h2;
if (!h2) return h1;
if (h1->val<=h2->val)
{
head = h1;
head->next=mergeLists(head->next,h2);
}
else
{
head = h2;
head->next=mergeLists(h1,head->next);
}
return head;
}