[LeetCode] Sort List
2015-06-02 23:47
405 查看
There are many merge-sort solutions at the forum, but very few quicksort solutions. So I post my accepted quicksort solution here.
Well, after reading the problem statement, I intuitively select quicksort since it is able to give an in-place solution and thus costs only constant space. Also, it is
Then I implement my quicksort solution and test it. I then submit it to the online judge. However, the annoying TLE error occurred. I check for the forums and some people suggested to use random pivoting or duplicate skipping. However, implementing random pivoting is a little costly, I lazily tried to skip the duplicates. And it works! So now comes the following solution . Note that each time I choose the first node as the pivot. Moreover, I create a
Of course, this solution passes the online judge luckily. If the linked list is like:
Well, after reading the problem statement, I intuitively select quicksort since it is able to give an in-place solution and thus costs only constant space. Also, it is
O(nlogn)in the expected case though it may become
O(n^2)in the worst case.
Then I implement my quicksort solution and test it. I then submit it to the online judge. However, the annoying TLE error occurred. I check for the forums and some people suggested to use random pivoting or duplicate skipping. However, implementing random pivoting is a little costly, I lazily tried to skip the duplicates. And it works! So now comes the following solution . Note that each time I choose the first node as the pivot. Moreover, I create a
new_headthat points to
headfor convenience.
Of course, this solution passes the online judge luckily. If the linked list is like:
100000 -> 99999 -> 99998 -> ... -> 1, it will fail since the subproblems only decrease by 1 at each recursion. However, it seems that the LeetCode OJ does not have this kind of test cases.
void sortListHelper(ListNode* head, ListNode* tail) { if (head -> next == tail) return; /* Partition the list. */ ListNode* pre = head; ListNode* cur = head -> next; ListNode* pivot = cur; while (cur -> next && cur -> next != tail) { if (pivot -> val > cur -> next -> val) { ListNode* temp = pre -> next; pre -> next = cur -> next; cur -> next = cur -> next -> next; pre -> next -> next = temp; } else cur = cur -> next; } sortListHelper(head, pivot); /* Here is the trick. */ while (pivot -> next != tail && pivot -> next -> val == pivot -> val) pivot = pivot -> next; if (pivot -> next != tail) sortListHelper(pivot, tail); } ListNode* sortList(ListNode* head) { ListNode* new_head = new ListNode(0); new_head -> next = head; sortListHelper(new_head, NULL); return new_head -> next; }
相关文章推荐
- @NotFound解决关联外键数据不存在时抛出异常的问题
- Part 72 to 81 Talking about Dictionary and List collection in C#
- 国嵌内核驱动进阶班-7-1(Ioctl设备控制)
- HTML5
- norflash与nandflash芯片内执行(XIP)
- java两个变量交换值不借助第三个变量所想
- [POJ 1328] Radar Installation
- Task 6.3 冲刺Two之站立会议2
- 实现文本的自动移动
- [POJ 2376] Cleaning Shifts
- http服务(一)――apache工作模式
- python impl模式
- 软件工程师薪福指南
- python中的map和reduce学习
- CUDA ---- Branch Divergence and Unrolling Loop
- Add and Search Word - Data structure design
- kyeremal-bzoj2038-[2009国家集训队]-小z的袜子(hose)-莫队算法
- JAVA生成UUID简介
- 做一个公益网站要花费多少钱
- 同步调用和异步调用同时存在导致的混乱