Insertion Sort List 2020-09-17 05:18
public ListNode insertionSortList(ListNode head) {
ListNode dummy = new ListNode(0);
ListNode pre;
dummy.next = head;
while (head != null && head.next != null) {
if (head.val <= head.next.val) {
head = head.next;
continue;
}
pre = dummy;
while (pre.next.val < head.next.val) {
pre = pre.next;
}
ListNode curr = head.next;
head.next = curr.next;
curr.next= pre.next;
pre.next = curr;
}
return dummy.next;
}
Runtime | Memory |
---|---|
6 ms | 38.5 MB |
EOF