描述
在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序。
您在真实的面试中是否遇到过这个题?
样例
给出 1->3->2->null,给它排序变成 1->2->3->null.
挑战
在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序。
您在真实的面试中是否遇到过这个题?
样例
给出 1->3->2->null,给它排序变成 1->2->3->null.
挑战
分别用归并排序和快速排序做一遍。
分析
快速排序:算法只交换节点的val值,平均时间复杂度O(nlogn),不考虑递归栈空间的话空间复杂度是O(1))。这里我们还需要注意的一点是数组的partition两个参数分别代表数组的起始位置,两边都是闭区间。
对左边子数组排序时,子数组右边界是middle-1,如果链表也按这种两边都是闭区间的话,找到分割后枢纽元middle,找到middle-1还得再次遍历数组,因此链表的partition采用前闭后开的区间(这样排序主函数也需要前闭后开区间)。
归并排序:算法交换链表节点,时间复杂度O(nlogn),不考虑递归栈空间的话空间复杂度是O(1))。
首先用快慢指针的方法找到链表中间节点,然后递归的对两个子链表排序,把两个排好序的子链表合并成一条有序的链表。归并排序应该算是链表排序最佳的选择了,保证了最好和最坏时间复杂度都是nlogn,而且它在数组排序中广受诟病的空间复杂度在链表排序中也从O(n)降到了O(1)。
程序
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list, using constant space complexity.
*/
//方法一:归并排序
ListNode * sortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//链表归并排序
if(head == NULL || head->next == NULL)return head;
else
{
//快慢指针找到中间节点
ListNode *fast = head,*slow = head;
while(fast->next != NULL && fast->next->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
fast = slow;
slow = slow->next;
fast->next = NULL;
fast = sortList(head);//前半段排序
slow = sortList(slow);//后半段排序
return merge(fast,slow);
}
}
// merge two sorted list to one
ListNode *merge(ListNode *head1, ListNode *head2)
{
if(head1 == NULL)return head2;
if(head2 == NULL)return head1;
ListNode *res , *p ;
if(head1->val < head2->val)
{res = head1; head1 = head1->next;}
else{res = head2; head2 = head2->next;}
p = res;
while(head1 != NULL && head2 != NULL)
{
if(head1->val < head2->val)
{
p->next = head1;
head1 = head1->next;
}
else
{
p->next = head2;
head2 = head2->next;
}
p = p->next;
}
if(head1 != NULL)p->next = head1;
else if(head2 != NULL)p->next = head2;
return res;
}
//方法二:快速排序
ListNode * sortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//链表快速排序
if(head == NULL || head->next == NULL)return head;
qsortList(head, NULL);
return head;
}
void qsortList(ListNode*head, ListNode*tail)
{
//链表范围是[low, high)
if(head != tail && head->next != tail)
{
ListNode* mid = partitionList(head, tail);
qsortList(head, mid);
qsortList(mid->next, tail);
}
}
ListNode* partitionList(ListNode*low, ListNode*high)
{
//链表范围是[low, high)
int key = low->val;
ListNode* loc = low;
for(ListNode*i = low->next; i != high; i = i->next)
if(i->val < key)
{
loc = loc->next;
swap(i->val, loc->val);
}
swap(loc->val, low->val);
return loc;
}
};