147. Insertion Sort List

来源:互联网 发布:eagle软件下载 编辑:程序博客网 时间:2024/06/06 00:49



public ListNode insertionSortList(ListNode head) {
if(head==null){
return null;
}
if(head.next==null){
return head;
}
ListNode rehead=new ListNode(-1);
rehead.next=head;
ListNode pre=head;
ListNode next=head.next;
while(next!=null){
if(pre.val<=next.val){
pre=pre.next;
next=pre.next;
}
else{
ListNode tmp=rehead;
while(tmp.next.val<=next.val){
tmp=tmp.next;
}
pre.next=next.next;
next.next=tmp.next;
tmp.next=next;
next=pre.next;
}
}

return rehead.next;


}

原创粉丝点击