问题描述:
Sort a linked list using insertion sort.
解析:
插入排序,常规算法。
public ListNode insertionSortList(ListNode head) {
ListNode temp=null;
ListNode root=new ListNode(Integer.MIN_VALUE);
root.next=temp;
ListNode flag=null;
ListNode parent=null;
if(head==null)
return null;
else{
while(head!=null){
flag=head;
parent=root;
temp=parent.next;
while((temp!=null)&&(temp.val<head.val)){
parent=parent.next;
temp=temp.next;
}
head=head.next;
flag.next=temp;
parent.next=flag;
}
}
return root.next;
}