Sort List
来源:互联网 发布:焦作淘宝实体店地 编辑:程序博客网 时间:2024/06/14 23:31
题目
Sort a linked list in O(n log n) time using constant space complexity.
方法
public static int sum = 0;/** * Definition for singly-linked list. * Sort a linked list in O(n log n) time using constant space complexity. */ public ListNode sortList(ListNode head) { ListNode top = new ListNode(0); top.next = head; quickSort(top,null); return top.next; } private void quickSort(ListNode top, ListNode end) { ListNode priorStart = top.next; ListNode priorEnd = top.next; if (priorStart != end && priorStart.next != end ) { ListNode forward = priorStart; ListNode backward = priorStart; ListNode help = priorStart.next; while (help != end) { ListNode cur = help; help = cur.next; if (cur.val > priorStart.val) {backward.next = cur;backward = cur;} else if (cur.val < priorStart.val){cur.next = forward;forward = cur;} else {if (backward == priorEnd) {backward.next = cur;priorEnd.next = cur;backward = cur;priorEnd = cur;} else {ListNode temp = priorEnd.next;cur.next = temp;priorEnd.next = cur;priorEnd = cur;}} } top.next = forward; backward.next = end; quickSort(top, priorStart); quickSort(priorEnd, end); } }
0 0
- list sort
- list sort
- Sort List
- Sort List
- list - sort
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- Sort List
- struts2之HelloWorld
- 5 多态 抽象类 接口
- DoubleTree
- glibc中malloc的详细解释
- 【UVa】10917 A Walk Through the Forest 最短路+DP
- Sort List
- 编译Busybox
- 【svn问题】svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- wxWidgets中从xrc文件中获取菜单选项的指针
- 同步与异步Socket
- jetty修改默认编码
- 用iostat对linux硬盘IO性能进行检测
- Evaluate Reverse Polish Notation
- CodeForces 20B Equation 水题