[LeetCode] Sort List

简介: There are many merge-sort solutions at the forum, but very few quicksort solutions. So I post my accepted quicksort solution here.

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 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_head that 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.

 1     void sortListHelper(ListNode* head, ListNode* tail) {
 2         if (head -> next == tail) return;
 3         /* Partition the list. */
 4         ListNode* pre = head;
 5         ListNode* cur = head -> next;
 6         ListNode* pivot = cur;
 7         while (cur -> next && cur -> next != tail) {        
 8             if (pivot -> val > cur -> next -> val) {
 9                 ListNode* temp = pre -> next;
10                 pre -> next = cur -> next;
11                 cur -> next = cur -> next -> next;
12                 pre -> next -> next = temp;
13             }
14             else cur = cur -> next;
15         }
16         sortListHelper(head, pivot);
17         /* Here is the trick. */
18         while (pivot -> next != tail && pivot -> next -> val == pivot -> val)
19             pivot = pivot -> next;
20         if (pivot -> next != tail) sortListHelper(pivot, tail);
21     } 
22 
23     ListNode* sortList(ListNode* head) {
24         ListNode* new_head = new ListNode(0);
25         new_head -> next = head;
26         sortListHelper(new_head, NULL);
27         return new_head -> next;
28     }

 

目录
相关文章
|
6月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
21 1
|
6月前
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
28 0
|
6月前
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
21 0
|
5月前
|
C++ 容器
【C++】STL容器——探究List与Vector在使用sort函数排序的区别(14)
【C++】STL容器——探究List与Vector在使用sort函数排序的区别(14)
|
5月前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
28 0
|
10月前
|
算法 C++
SGI-STL源码剖析之list::sort()
SGI-STL源码剖析之list::sort()
79 0
LeetCode 75 Sort Colors 颜色分类(荷兰国旗)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
69 0
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List