[刷题]Sort List
来源:互联网 发布:usda数据查询 编辑:程序博客网 时间:2024/05/17 03:11
[LintCode]Sort List
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The head of linked list. * @return: You should return the head of the sorted linked list, using constant space complexity. */ public ListNode sortList(ListNode head) { // 2015-5-24 O(nlogn) 分治法 递归 类似归并排序 // exit condition if (head == null || head.next == null) { return head; } ListNode mid = findMid(head); ListNode listB = sortList(mid.next); mid.next = null; ListNode listA = sortList(head); // listB 不长于 listA return mergeList(listA, listB); } private ListNode findMid(ListNode head) { if (head == null) { return head; } ListNode slow = head; ListNode fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } private ListNode mergeList(ListNode listA, ListNode listB) { ListNode dummy = new ListNode(0); ListNode tail = dummy; while (listA != null && listB != null) { if (listA.val < listB.val) { tail.next = listA; listA = listA.next; } else { tail.next = listB; listB = listB.next; } tail = tail.next; } if (listA != null) { tail.next = listA; } if (listB != null) { tail.next = listB; } return dummy.next; }}
0 0
- Sort List leetcode刷题
- [刷题]Sort List
- [刷题]Insertion Sort List
- insertion sort list刷题 leetcode
- [LeetCode刷题] Insertion Sort List
- 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
- 问题:java Swing 文件管理
- hibernate重要查询语句
- 青岛吸费电话设备工厂 增值电话平台 小成本高利润
- pyhon学习
- Linux 文件锁与 inode
- [刷题]Sort List
- 易学c++链表学习
- 烟台加值电话招商 声讯电话平台 月入过万
- freeradius 中配置eap-peap
- 1009. 说反话
- 淄博手机吸费代理 揭秘吸费电话 轻松赚钱
- erlang的日志库 A logging framework for Erlang/OTP
- 深圳真的电话吸费 手机吸费代理 电话群呼器
- 类模板