We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 4256dcf + bc385d0 commit abb05f5Copy full SHA for abb05f5
java/0147-insertion-sort-list.java
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ public ListNode insertionSortList(ListNode head) {
3
+ ListNode dummy = new ListNode(0, head);
4
+
5
+ ListNode prev = head;
6
+ ListNode cur = head.next;
7
+ while (cur != null) {
8
+ if (prev.val <= cur.val) {
9
+ prev = cur;
10
+ cur = cur.next;
11
+ } else {
12
+ ListNode elem = dummy;
13
+ while (elem.next.val < cur.val) {
14
+ elem = elem.next;
15
+ }
16
17
+ prev.next = cur.next;
18
+ cur.next = elem.next;
19
+ elem.next = cur;
20
21
+ cur = prev.next;
22
23
24
25
+ return dummy.next;
26
27
+}
0 commit comments