-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.java
More file actions
37 lines (33 loc) · 890 Bytes
/
insertionSort.java
File metadata and controls
37 lines (33 loc) · 890 Bytes
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
34
35
36
37
c ListNode insertionSortList(ListNode a) {
if(a==null || a.next ==null)
return a;
ListNode b = new ListNode(a.val);
a=a.next;
while(a!=null)
{
ListNode c = b;
ListNode prec =null;
while(c!=null)
{
if(c.val <a.val) {
prec = c;
c = c.next;
}
else {
ListNode newNode = new ListNode(a.val);
if(prec !=null)
prec.next = newNode;
else
b = newNode;
newNode.next=c;
break;
}
}
if(c==null)
{
prec.next=new ListNode(a.val);
}
a=a.next;
}
return b;
}