Sort a linked list in O(n log n) time using constant space complexity.
来源:互联网 发布:高速公路车流量数据 编辑:程序博客网 时间:2024/05/29 18:36
时间复杂度要求O(nlogn),可用快排,归并排,堆排来实现。
1,快排。快排基于分治法,取中枢值pivot作划分partition,一趟partition()操作后,中枢值位置定,表中元素被中枢值一分为二,递归划分两子表。当然划分函数partition()很重要。
代码实现:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
return quickSort(head,NULL);
}
ListNode* quickSort(ListNode *head,ListNode *end){
if (head!=end){
ListNode* pivot = partion(head, end);//返回中枢值的指针
quickSort(head,pivot);
quickSort(pivot->next,end);
}
return head;
}
ListNode* partion(ListNode *head,ListNode *end){
int key = head->val;
ListNode *p = head;
ListNode *q = head->next;
while(q!=end){
if(q->val<key){
p = p->next;
swap(p,q);
}
q = q->next;
}
swap(head,p);
return p;
}
void swap(ListNode *p,ListNode *q){
int temp = p->val;
p->val = q->val;
q->val = temp;
}
};
2,归并排。
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if(!head || !head->next){
return head;
}
ListNode *p = head;
ListNode *q = head->next;
while(q && q->next){//快慢指针思路,快指针一次走两步,慢指针一次走一步,快指针在链表末尾时,慢指针恰好在链表中点
p = p->next;
q = q->next->next;
}
ListNode *r = sortList(p->next);
p->next = NULL;
ListNode *l = sortList(head);
return merge(l,r);
}
ListNode *merge(ListNode *left,ListNode *right){//写出merge函数,即如何合并链表。
ListNode dummy(0); //创建结构体dummy(0)
ListNode *p = &dummy;
while(left && right){
if (left->val < right->val){
p->next = left;
left = left->next;
}else{
p->next = right;
right = right->next;
}
p = p->next;
}
if (left) p->next = left;
if (right) p->next = right;
return dummy.next;//结构体取属性
}
};
- Sort a linked list in O(n log n) time using constant space complexity
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort List (Sort a linked list in O(n log n) time using constant space complexity)
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- separate odd and even numbers in an array in O(n) time complexity and O(1) space complexity
- LeetCode234——Palindrome Linked List,O(n) time and O(1) space
- Solution to Maximum Subarray in linear-time algorithm with time complexity is O(n)
- Reverse a doubly linked list in O(1) time
- Sort a linked list using insertion sort
- Sort a linked list using insertion sort
- Find duplicates in O(n) time and O(1) extra space
- leetcode_Sort a linked list using insertion sort.
- 算法的基础知识( Time Complexity & Space Complexity& Big O notation)
- ASP.NET(C#)图片加文字、图片水印
- 在每次request请求时变化session
- Android AIDL 详解
- 堆的应用
- [FAQ08887]如何使用Trace32分析NE问题?
- Sort a linked list in O(n log n) time using constant space complexity.
- 第四周项目3(1)-单链表应用 逆置单链表
- poj1014多重背包
- 两个listview之间焦点切换, 如何将焦点落在之前所处位置
- @Resource和@Autowired的区别
- 1. java基础(个人小总结)(5)
- 第四周【项目7-单链表应用举例】
- SAP 标准金税接口方案介绍
- [FAQ10640]如何调试在dalvik-jit中抛出的SIGSEGV类型的NE