-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetCode-147-Insertion-Sort-List.java
76 lines (59 loc) · 2.23 KB
/
LeetCode-147-Insertion-Sort-List.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
// Insert curr node to current node
// public ListNode insertionSortList(ListNode head) {
// ListNode dummy = new ListNode(-1);
// dummy.next = head;
// ListNode prevCurr = dummy;
// while (prevCurr != null && prevCurr.next != null) {
// ListNode curr = prevCurr.next;
// ListNode prev = dummy;
// while(prev.next != null && prev.next.val < curr.val) {
// prev = prev.next;
// }
// if (prev.next != curr) {
// ListNode next = curr.next;
// curr.next = prev.next;
// prev.next = curr;
// prevCurr.next = next;
// } else {
// prevCurr = prevCurr.next;
// }
// }
// return dummy.next;
// }
// Simple and easy. Insert curr node to a new list
public ListNode insertionSortList(ListNode head) {
if(head == null) return head;
ListNode dummy = new ListNode(-1); // the pre-head node of the new list
ListNode curr = head; // the current node to insert to new list
while (curr != null) {
ListNode next = curr.next;
// find the location to insert in new list: between prev and prev.next
ListNode prev = dummy;
while (prev.next != null && prev.next.val <= curr.val) prev = prev.next;
// insert curr to prev and prev.next
curr.next = prev.next;
prev.next = curr;
curr = next;
}
return dummy.next;
}
private void printList(ListNode head) {
StringBuilder sb = new StringBuilder();
while (head != null) {
sb.append(head.val).append(" ");
head = head.next;
}
System.out.println(sb.toString());
}
}