Sort List 链表排序@LeetCode
来源:互联网 发布:网络咨询师技巧 编辑:程序博客网 时间:2024/04/27 16:22
要求时间为O(nlogn),最适合就是merge sort,每次从n/2处断开,对两段递归sort,然后再merge起来。
空间是O(logn)用了栈空间
package Level4;import Utility.ListNode;/** * Sort List * * Sort a linked list in O(n log n) time using constant space complexity. * */public class S144 {public static void main(String[] args) {int[] list = {1,4,2,3};ListNode head = ListNode.create(list);ListNode h = sortList(head);h.print();}public static ListNode sortList(ListNode head) {if(head==null || head.next==null){// 链表没有元素或是只有一个元素的情况直接返回return head;}ListNode fast = head;ListNode slow = head;ListNode preSlow = head;// 找到中间节点的前一个while(fast!=null && fast.next!=null){fast = fast.next.next;preSlow = slow;slow = slow.next;}//System.out.println(preSlow.val);// 断开,分成两段preSlow.next = null;ListNode first = sortList(head);// 得到以排序好的前半段ListNode second = sortList(slow);// 得到以排序好的后半段ListNode dummy = new ListNode(-1);ListNode dummyCur = dummy;while(first!=null && second!=null){// 合并两半段if(first.val<second.val){dummyCur.next = first;first = first.next;}else if(second.val<=first.val){dummyCur.next = second;second = second.next;}dummyCur = dummyCur.next;}while(first != null){dummyCur.next = first;first = first.next;dummyCur = dummyCur.next;}while(second != null){dummyCur.next = second;second = second.next;dummyCur = dummyCur.next;}return dummy.next; }}
Merge Sort:
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public static ListNode sortList(ListNode head) {if (head == null || head.next == null) {return head;}ListNode slow = head, fast = head, preSlow = head;while (fast != null && fast.next != null) {preSlow = slow;slow = slow.next;fast = fast.next.next;}preSlow.next = null;ListNode h1 = sortList(head);ListNode h2 = sortList(slow);return mergeList(h1, h2);}public static ListNode mergeList(ListNode h1, ListNode h2) {ListNode dummyHead = new ListNode(0);ListNode dummy = dummyHead;while (h1 != null && h2 != null) {if (h1.val <= h2.val) {dummy.next = h1;h1 = h1.next;} else {dummy.next = h2;h2 = h2.next;}dummy = dummy.next;}while (h1 != null) {dummy.next = h1;h1 = h1.next;dummy = dummy.next;}while (h2 != null) {dummy.next = h2;h2 = h2.next;dummy = dummy.next;}return dummyHead.next;}}
0 0
- Sort List 链表排序@LeetCode
- Leetcode Sort List 链表归并排序
- LeetCode Sort List(链表排序)
- LeetCode-Sort List 链表排序
- Leetcode Sort List 链表排序
- LeetCode Sort List(链表排序)
- 【LeetCode】Sort List 链表排序- Medium ++(Merge&Quick Sort)
- leetcode题解-链表排序算法 147. Insertion Sort List && 148 . Sort List
- Insertion Sort List 链表插入排序@LeetCode
- LeetCode:Insertion Sort List//链表插入排序
- leetcode Insertion Sort List(链表插入排序)
- LeetCode | Insertion Sort List(插入法排序链表)
- leetcode:Sort List(链表的归并排序)
- LeetCode OJ 之 Sort List (排序链表)
- [C++]LeetCode: 125 Sort List (归并排序链表)
- [C++]LeetCode: 126 Insertion Sort List (插入排序链表)
- Leetcode Insertion Sort List 插入排序链表
- LeetCode Insertion Sort List 链表的插入排序
- 谁说ios不可以自定义控件?(可用xib拖动的)
- PHP中substr() mb_substr() mb_struct()的区别和用法
- VC-刺梨--百度百科
- servlet中字节流和字符流不能同时使用
- 深入浅出学Spring Data JPA
- Sort List 链表排序@LeetCode
- Linux内存点滴 用户进程内存空间
- LeetCode | Palindrome Partitioning II
- 网页中通过js修改img的src属性刷新图片时,图片缓存问题现象表述及问题解决
- document.write()和document.writeln()的区别,实现换行
- C语言考试大纲(参考)
- isPostback
- java单例模式
- ACE_Log_Msg日志