Sort List (大的还没跑通)

来源:互联网 发布:网络剪刀手win7中文版 编辑:程序博客网 时间:2024/05/30 05:40
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;    }}