[Leetcode]Sort List
来源:互联网 发布:2016淘宝各类目转化率 编辑:程序博客网 时间:2024/05/17 23:57
Sort a linked list in O(n log n) time using constant space complexity.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: /*algorithm: quick sort 1: divide list to two : l1,l2 2: sort l1, l2 recrusive 3: merge l1,l2 */ ListNode* sortList(ListNode* head) { if(!head)return NULL; if(!head->next)return head; ListNode* faster,*slower; faster = slower = head; while(faster && faster->next && faster->next->next){ faster = faster->next->next; slower = slower->next; } ListNode* l1 = head,*l2 = slower?slower->next:NULL; if(slower)slower->next = NULL; l1 = sortList(l1); l2 = sortList(l2); //merge l1,l2 ListNode dummy(0),*tail = &dummy; while(l1&&l2){ if(l1->val < l2->val){ tail->next = l1; l1 = l1->next; }else{ tail->next = l2; l2 = l2->next; } tail = tail->next; } if(l1)tail->next = l1; else tail->next = l2; return dummy.next; }};
0 0
- [leetcode][list][sort] 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
- [LeetCode] Sort List
- 【LeetCode】Insertion Sort List
- leetCode - Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- 新版MySql 5.6.20 for win32,安装后空密码不能登陆
- iOS中block的探究
- git打包
- Android:shape的使用详解(1)
- iOS开发基础技能之数据库(SQLite)
- [Leetcode]Sort List
- Android Studio 1.4带来的福利
- UI设计
- 处理大数据的四个步骤
- Java PBKDF2 密码哈希代码 安全性较高的加密
- Objective-C @encode关键字
- switch Value 拨码开关
- 图像的灰度直方图介绍
- Windows 10 使用心得