-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertSort.java
33 lines (33 loc) · 917 Bytes
/
InsertSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class InsertSort {
public ListNode insertionSortList(ListNode head) {
if(head==null || head.next==null){
return head;
}
ListNode cur = head;
ListNode first=head;
ListNode prev = null;
while(cur.next!=null){
if(cur.next.val<cur.val){
ListNode temp=cur.next;
cur.next = cur.next.next;
first = head;
prev=null;
while(first.val<temp.val){
prev = first;
first=first.next;
}
if(prev==null){
temp.next=head;
head=temp;
}
else{
prev.next = temp;
temp.next=first;
}
cur=head;
}
cur=cur.next;
}
return head;
}
}