-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (35 loc) · 1001 Bytes
/
Solution.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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = dummy.next,
pre = cur;
while (cur != null) {
if (cur.val < pre.val) {
ListNode next = cur.next;
pre.next = next;
ListNode p1 = dummy,
p2 = p1.next;
while (p2.val < cur.val) {
p1 = p1.next;
p2 = p2.next;
}
cur.next = p2;
p1.next = cur;
cur = next;
} else {
pre = cur;
cur = cur.next;
}
}
return dummy.next;
}
}