leetCode(5):Sort List
来源:互联网 发布:请解释高斯滤波的算法 编辑:程序博客网 时间:2024/06/14 05:43
Sort a linked list in O(n log n) time using constant space complexity.
分析:排序算法中,堆排序、归并排序、快速排序、希尔排序的时间复杂度是nlogn,堆排序和归并排序对下标依赖性比较强,比较适合顺序表的排序,对链表处理起来比较复杂。希尔排序用的比较少。所以我选择的是快速排序,结果是正确的,但时间超出限制了。如果有大神做过同样的题目,跪求告知解法~~~~
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* partion(ListNode* head,ListNode* start,ListNode* end,ListNode** newEnd) { int key=end->val; ListNode* pre=start; ListNode* behind=start; *newEnd=behind; while(pre!=end) { if(pre->val < key) { if(pre!=behind) { int tmp=pre->val; pre->val=behind->val; behind->val=tmp; } *newEnd=behind; behind=behind->next; } pre=pre->next; } int tmp=pre->val; pre->val=behind->val; behind->val=tmp; return behind; } void qSort(ListNode* head,ListNode* start,ListNode* end) { if(start!=end) { ListNode* newEnd=NULL; ListNode* mid=partion(head,start,end,&newEnd); if(mid!=end) qSort(head,mid->next,end); qSort(head,start,newEnd); } } ListNode* sortList(ListNode* head) { if(head==NULL) return NULL; ListNode* p=head; while(p->next) p=p->next; qSort(head,head,p); return head; }};
0 0
- leetCode(5):Sort List
- LeetCode 5 Insertion Sort List
- [leetcode][list][sort] Sort List
- LeetCode problem 5: Insertion Sort List
- LeetCode题目5:Insertion Sort List
- leetcode系列(5)Insertion Sort List
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Leetcode: Insertion Sort List
- Leetcode: Sort List
- Sort List | leetcode
- leetcode-Sort List
- leetcode-Insertion Sort List
- LeetCode题解:Sort List
- leetcode 134: Sort List
- [LeetCode]Sort List
- [LeetCode]Insertion Sort List
- input框只能输入大于0的数字,当值为0或者“”时会变成1
- j2se学习笔记-Enum枚举类型
- JS学习笔记(一):关于{}和[]的使用
- android代码混淆之自定View问题
- HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.pages.index.index_jsp
- leetCode(5):Sort List
- 使用MyEclipse+TomCat搭建服务端开发环境
- android自定义Switch样式
- PAT Basic 1033. 旧键盘打字(20)
- MyCAT监控命令
- 【Android】下拉列表、拖动条、星级评分条与标签文本的触摸事件
- MDK中One ELF Section per Function选项功能探究
- MySQL双机热备份方法
- java大数据运算