leetcode Sort a linked list in O(n log n) time using constant space complexity.
来源:互联网 发布:java 多线程 set slep 编辑:程序博客网 时间:2024/06/05 07:19
分析:使用o(nlogn)的时间复杂度和常量空间复杂度,对链表排序,只能使用归并排序。
归并排序是将两个或两个以上的有序链表合并成一个新的链表。
常见的是二路归并排序算法,思想是将数组或链表中前后相邻的两个有序序列归并为一个有序序列,
时间复杂度为o(nlogn),需要等数量的辅助空间。
下面的源码,用到的技巧有快慢指针,获取链表的中间指针。
链表存储的归并排序 时间复杂度O(nlogn)空间复杂度 O(1)
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { //快速排序,空间复杂度为O(n) /* public ListNode sortList(ListNode head) { quickSort(head,null); return head; } public static void quickSort(ListNode head, ListNode end){ if (head != end){ ListNode partion = partion(head); quickSort(head,partion); quickSort(partion.next,end); } } public static ListNode partion(ListNode head){ ListNode slow = head; ListNode fast = head.next; while (fast != null){ if (fast.val < head.val){ slow = slow.next; fast.val = slow.val ^ fast.val ^(slow.val = fast.val); } fast = fast.next; } slow.val = head.val ^ slow.val ^ (head.val = slow.val); return slow; }*/ //归并排序 public ListNode sortList(ListNode head) { if (head == null || head.next == null){ return head; } ListNode fast = head; ListNode slow = head; while (fast != null && fast.next != null && fast.next.next != null){ fast = fast.next.next; slow = slow.next; } fast = slow ; slow = slow.next; fast.next = null ; fast = sortList(head); slow = sortList(slow); return merge(fast,slow); } private ListNode merge(ListNode sub1, ListNode sub2){ if (sub1 == null){ return sub2; } if (sub2 == null){ return sub1; } ListNode head; if (sub1.val < sub2.val ){ head = sub1; sub1 = sub1.next; }else{ head = sub2; sub2 = sub2.next; } ListNode p = head; while (sub1 != null && sub2 != null){ if (sub1.val < sub2.val){ p.next = sub1; sub1 = sub1.next; }else{ p.next = sub2; sub2 = sub2.next; } p = p.next; } if(sub1 != null){ p.next = sub1; } if(sub2 != null){ p.next = sub2; } return head; }}
阅读全文
0 0
- leetcode Sort a linked list in O(n log n) time using constant space complexity.
- leetcode Sort List (Sort a linked list in O(n log n) time using constant space complexity)
- Sort a linked list in O(n log n) time using constant space complexity
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- separate odd and even numbers in an array in O(n) time complexity and O(1) space complexity
- LeetCode234——Palindrome Linked List,O(n) time and O(1) space
- Solution to Maximum Subarray in linear-time algorithm with time complexity is O(n)
- 【Leetcode】Sort List in O(nlogn) O(1)space
- leetcode上的Sort a linked list using insertion sort
- Reverse a doubly linked list in O(1) time
- LeetCode Reorder List O(n) space空间解法
- Sort a linked list using insertion sort
- Sort a linked list using insertion sort
- WaitForSingleObject的详细用法
- vector<int>与vector<int>::iterator
- 图解SQL之left join、right join、inner join的区别
- Eclipse快捷键大全
- solr分词测试
- leetcode Sort a linked list in O(n log n) time using constant space complexity.
- LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
- oracle日常操作
- windows 批处理 获取当前目录及子目录的所有文件路径
- python中namedtuple的用法
- PIM独立组播协议
- bzoj4337: BJOI2015 树的同构 树hash
- 程序员必备的600个英语词汇合集(1)
- 对象序列化中多次写入文件头的问题