LeetCode —— Sort List

做了一段時間的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;
	    }
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章