LeetCode —— Sort List
来源:互联网 发布:网络音视频许可证 编辑:程序博客网 时间:2024/06/03 05:06
做了一段时间的leetcode,别看leetcode的算法基础,边界条件cover的非常全面,
题目要求时间复杂度O(n log n),空间复杂度为常量,首先想到快速排序,堆排序,但这两种算法比较适用于数组,
就决定用非常基本的并归排序算法,通过调整node的位置,进行二路合并
思路虽然简单,遇到相同 val的节点,一不小心会丢失,终于找到了原因,分析并通过,总结一下,帮助提高。
public class Solution { public ListNode sortList(ListNode head) { if(head==null||head.next ==null)return head; ListNode ans = null; ListNode slow = null,fast = null; for(slow = head,fast = head;fast.next != null&&fast.next.next != null; slow = slow.next,fast = fast.next.next); ListNode head2 = slow.next; slow.next = null; if(head != slow){ ans = merge(head,head2); }else{ if(head.val>head2.val){ ans = head2; head2.next = head; }else{ head.next = head2; ans = head; } } return ans; } ListNode merge(ListNode head1,ListNode head2){ ListNode slow = null,fast = null; if(head1.next == null){ //*************one node in list1, do nothing********** }else if(head1.next.next == null){ //*************two node in list1, judge and adjust********* if(head1.val > head1.next.val){ head1.next.next = head1; head1 = head1.next; head1.next.next = null; } }else{ //***************more than two node in list1, recursion********* for(slow = head1,fast = head1;fast.next != null&&fast.next.next != null; slow = slow.next,fast = fast.next.next); ListNode tmp = slow.next; slow.next = null; head1 = merge(head1,tmp); } if(head2.next == null){ //*************one node in list2, do nothing********** }else if(head2.next.next==null){ //*************two node in list2, judge and adjust********* if(head2.val > head2.next.val){ head2.next.next = head2; head2 = head2.next; head2.next.next=null; } }else{ //***************more than two node in list2, recursion********* for(slow = head2,fast = head2;fast.next != null&&fast.next.next != null; slow = slow.next,fast = fast.next.next); ListNode tmp = slow.next; slow.next = null; head2 = merge(head2,tmp); } ListNode ans = null; if(head1.val > head2.val) ans = head2; else ans = head1; while(head1!=null && head2!=null){ ListNode tmp = null; /**** * 在合并阶段,非常关键,小心丢失相同元素 * head1.val == head2.val 时,head1向前推进; * 所以当head1.next.val == head2.val, 不能改变head1.next,直接后移 * 当head2.next.val == head1.val, 要改变head2.next指向head1,head2再向后移。 */ if(head1.val > head2.val) { tmp = head2.next; if(tmp!=null){ if(tmp.val < head1.val){ //这里不能有 = head2 = head2.next; }else{ head2.next = head1; head2 = tmp; } }else{ head2.next = head1; head2 = null; } }else{ tmp = head1.next; if(tmp!=null){ if(tmp.val <= head2.val){//head1.next.val == head2.val 时 , head1后移但不改变head1.next指向head2 head1 = head1.next; //这里要有 = }else{ head1.next = head2; head1 = tmp; } }else{ head1.next = head2; head1 = null; } } } return ans; } }
0 0
- LeetCode—Sort List
- LeetCode——Sort List
- LeetCode —— Sort List
- LeetCode——Sort List
- LeetCode—Insertion Sort List
- LeetCode——Insertion Sort List
- LeetCode——Insertion Sort List
- LeetCode题解——Sort List
- leetcode 147 —— Insertion Sort List
- leetcode 148 —— Sort List
- Insertion Sort List —— Leetcode
- LeetCode—Sort List解题报告
- LeetCode—Insertion Sort List 解题报告
- LeetCode 之 Sort List — C 实现
- LeetCode—147 Insertion Sort List
- [leetcode][list][sort] Sort List
- LeetCode刷题(C++)——Sort List(Medium)
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- POJ 1322 Chocolate
- Windows7 64位和32位有何差异
- POJ 3827 Facer is learning to swim
- 字符设备程序实列二-查询按键值,按键按下相应的LED灯亮,按键松开相应的LED灯灭
- java.util.concurrent包(1)-lock和synchronized对比
- LeetCode —— Sort List
- 虚基类--动物学。。。。
- USB mass storage framework
- NYOJ-60 谁获得了最高奖学金
- Oracle操作XML各种场景介绍
- 用antlr4来实现《按编译原理的思路设计的一个计算器》中的计算器
- 条款16:成对使用new和delete时要采取相同形式
- java中hashcode是怎么算出来的?
- struts的MVC和spring的MVC的区别