leetcode系列(2)链表排序
来源:互联网 发布:java 短信防刷 编辑:程序博客网 时间:2024/05/23 16:50
Sort a linked list in O(n log n) time using constant space complexity.
咋一看貌似很简单,直接用链表的快速排序,结果爆出time limit exceed,这不科学啊,后来一想快排有退化的可能,如下表:
归并排序(稳定)O(nlogn)O(nlogn)O(nlogn)快速排序(不稳定)O(nlogn)O(nlogn)O(n^2)堆排序(不稳定)O(nlogn)O(nlogn)O(nlogn)所以觉得用归并排序,主要就是分裂链表,和合并有序链表(用递归可以方便的实现),提交后马上AC。
贴上我的自己的xcode上的代码,包含了两种排序方法。
//// qsort_list.cpp// leet_code//// Created by meng duan on 14-4-3.// Copyright (c) 2014年 886524. All rights reserved.//#include "common.h"class QsortList {public: struct ListNode { int val; ListNode *next; ListNode (int x) : val(x), next(NULL) {} }; // 快速排序部分 ListNode *listPart(ListNode *head, ListNode *end) { ListNode *ret = head; ListNode *p1 = head; ListNode *p2 = head->next; int val = head->val; for ( ; p2 != end; p2 = p2->next) { if (p2->val < val) { int tmp = p2->val; p1 = p1->next; p2->val = p1->val; p1->val = tmp; } } head->val = p1->val; p1->val = val; ret = p1; return ret; } void listSort(ListNode *head, ListNode *end) { if (head != end && head->next != end) { ListNode *mid = listPart(head,end); listSort(head, mid); listSort(mid->next,end); } } ListNode *sortList (ListNode *head) { // if (head) { // listSort(head,NULL); // } // return head; if (head && head->next) { mergeSort(&head); } return head; } // 归并排序部分 void mergeSort(ListNode **head) { if (*head && (*head)->next) { ListNode *p1 = *head; ListNode *p2 = listSplit(*head); mergeSort(&p1); mergeSort(&p2); *head = mergeSortList(p1,p2); } } ListNode *listSplit(ListNode *head) { if (head && head->next) { ListNode *p1 = head; ListNode *p2 = head->next; while (p2) { p2 = p2->next; if (p2) { p1 = p1->next; p2 = p2->next; } } p2 = p1->next; p1->next = NULL; return p2; } return head; } ListNode *mergeSortList(ListNode *p1, ListNode *p2) { if (NULL == p1) { return p2; } else if (NULL == p2) { return p1; } else if (p1->val < p2->val) { p1->next = mergeSortList(p1->next, p2); return p1; } else { p2->next = mergeSortList(p1, p2->next); return p2; } } };int main (int argc, char **argv){ typedef QsortList::ListNode Node_t; Node_t *head = new Node_t(0); for (int i = 1; i < 5; ++i) { Node_t *node = new Node_t(i); node->next = head; head = node; } QsortList m; head = m.sortList(head); while (head) { cout<<head->val<<endl; head = head->next; } return 0;}
0 0
- leetcode系列(2)链表排序
- 【LeetCode】链表系列(排序)
- 【LeetCode】数组系列(排序)
- 【LeetCode】排序系列
- leetcode 链表排序
- 列表重新排序——Leetcode系列(八)
- LeetCode Sort List(链表排序)
- 链表排序问题(leetcode试题)
- 使用快速排序算法对列表进行排序——Leetcode系列(四)
- 使用插入排序算法对列表进行排序——Leetcode系列(五)
- [LeetCode] Valid Anagram - 字符串排序比较系列
- [LeetCode] Valid Anagram - 字符串排序比较系列
- 深度复制链表——Leetcode系列(十三)
- leetcode系列(16)判断链表是否为回文
- leetcode总结无止境系列之链表
- leetcode总结无止境系列之链表
- leetcode题目 反转链表系列问题
- Leetcode--easy系列2
- 学习Scala:函数字面量是如何实现的
- 我的第一篇博客
- Socket简介
- 编译和链接
- Lucene.net
- leetcode系列(2)链表排序
- 生活随笔:工作的做事方式?
- 园艺花卉产品采购推荐管理系统开发 已买产品管理检查查询工具
- 我的位置
- 登录验证的三种方式(控制台)
- STM32F103停机模式测试
- Linux对MySQL优化(从操作角度)
- Http Basic Authorizaition验证
- Objective-C iOS开发中常见的语句@synthesize obj=_obj的意义详解