-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode147.java
51 lines (40 loc) · 1.13 KB
/
leetcode147.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Leetcode147 {
public static void main(String[] args){
ListNode head = new ListNode(5);
head.next = new ListNode(3);
head.next.next = new ListNode(7);
head.next.next.next = new ListNode(9);
ListNode p = insertionSortList(head);
while(p!= null){
System.out.println(p.val);
p = p.next;
}
}
public static ListNode insertionSortList(ListNode head) {
if(head == null)
return null;
ListNode result = head;
head = head.next;
result.next = null;
ListNode temp; //从head链表上取下来一个节点。
ListNode p; //p用来在result链表中找到合适的位置,将temp插进去
while(head != null){
temp = head;
head = head.next;
temp.next = null;
if(temp.val<result.val){
temp.next = result;
result = temp;
}
else{
p = result;
while(p.next != null && temp.val>p.next.val){
p = p.next;
}
temp.next = p.next;
p.next = temp;
}
}
return result;
}
}