Sort List (大的还没跑通)

public class Solution {
    public ListNode sortList(ListNode head) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
       if(head==null||head.next==null)return head;
       
       if(length(head)<=10){
    	   ListNode begin = head;
    	   while(begin!=null){
    		   ListNode latter = begin.next;
    		   while(latter!=null){
    			   if(begin.val>latter.val){
    				   int temp=latter.val;
    				   latter.val=begin.val;
    				   begin.val=temp;
    			   }
    			   latter=latter.next;
    		   }
    		   begin=begin.next;
    	   }
    	   return begin;
       }
       
       ListNode fast=head;
       ListNode slow=head;
      /* while(fast.next != null && fast.next.next != null){
                 fast = fast.next.next;
                 slow = slow.next;
       }*/
       fast = slow.next;
       slow.next = null;
       
       slow = head;
       ListNode temp = slow;
       /*while(temp!=null){
    	   System.out.println("before sort slow val:"+temp.val);
    	   temp=temp.next;
       }
       */
       
       slow = sortList(slow);
       
        temp = slow;
       /*while(temp!=null){
    	   System.out.println("after sort slow val:"+temp.val);
    	   temp=temp.next;
       }*/
       temp =fast;
       /*while(temp!=null){
    	   System.out.println("before sort fast val:"+temp.val);
    	   temp=temp.next;
       }
*/   
       fast=sortList(fast);
       temp =fast;
       /*while(temp!=null){
    	   System.out.println("after sort fast val:"+temp.val);
    	   temp=temp.next;
       }*/
       head = merge(slow,fast);
      
       return head;
       
    }
   
   
    public static ListNode merge(ListNode l1,ListNode l2){
        if(l1==null)return l2;
        if(l2==null)return l1;
    	ListNode head =null;
    	ListNode temp = l1;
        /*while(temp!=null){
     	   System.out.println("before merge l1 val:"+temp.val);
     	   temp=temp.next;
        }*/
        temp = l2;
        /*while(temp!=null){
     	   System.out.println("before merge l2 val:"+temp.val);
     	   temp=temp.next;
        }*/
        
        if(l1.val>l2.val){
                head=l2;
                l2=l2.next;
         } else {
                head=l1;
                l1=l1.next;
        }
        ListNode current = head;
        while(l1!=null&&l2!=null){
            if(l1.val>l2.val){
                current.next=l2;
                l2=l2.next;
            } else {
                current.next=l1;
                l1=l1.next;
            }
            current=current.next;
        }
        if(l1!=null){
            current.next=l1;
        }
        if(l2!=null){
            current.next=l2;
        }
        temp = head;
        /*while(temp!=null){
      	   System.out.println("after merge val:"+temp.val);
      	   temp=temp.next;
         }*/
        
        return head;
    }
    public static int length(ListNode listnode){
    	int length = 0;
    	while(listnode!=null){
    		listnode=listnode.next;
    		length++;
    	}
    	return length;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值