File tree 3 files changed +109
-0
lines changed
solution/0100-0199/0147.Insertion Sort List
3 files changed +109
-0
lines changed Original file line number Diff line number Diff line change @@ -123,6 +123,44 @@ class Solution {
123
123
}
124
124
```
125
125
126
+ ### ** JavaScript**
127
+
128
+ ``` js
129
+ /**
130
+ * Definition for singly-linked list.
131
+ * function ListNode(val, next) {
132
+ * this.val = (val===undefined ? 0 : val)
133
+ * this.next = (next===undefined ? null : next)
134
+ * }
135
+ */
136
+ /**
137
+ * @param {ListNode} head
138
+ * @return {ListNode}
139
+ */
140
+ var insertionSortList = function (head ) {
141
+ if (head == null || head .next == null ) return head;
142
+ let dummy = new ListNode (head .val , head);
143
+ let prev = dummy, cur = head;
144
+ while (cur != null ) {
145
+ if (prev .val <= cur .val ) {
146
+ prev = cur;
147
+ cur = cur .next ;
148
+ continue ;
149
+ }
150
+ let p = dummy;
151
+ while (p .next .val <= cur .val ) {
152
+ p = p .next ;
153
+ }
154
+ let t = cur .next ;
155
+ cur .next = p .next ;
156
+ p .next = cur;
157
+ prev .next = t;
158
+ cur = t;
159
+ }
160
+ return dummy .next ;
161
+ };
162
+ ```
163
+
126
164
### ** ...**
127
165
128
166
```
Original file line number Diff line number Diff line change @@ -113,6 +113,44 @@ class Solution {
113
113
}
114
114
```
115
115
116
+ ### ** JavaScript**
117
+
118
+ ``` js
119
+ /**
120
+ * Definition for singly-linked list.
121
+ * function ListNode(val, next) {
122
+ * this.val = (val===undefined ? 0 : val)
123
+ * this.next = (next===undefined ? null : next)
124
+ * }
125
+ */
126
+ /**
127
+ * @param {ListNode} head
128
+ * @return {ListNode}
129
+ */
130
+ var insertionSortList = function (head ) {
131
+ if (head == null || head .next == null ) return head;
132
+ let dummy = new ListNode (head .val , head);
133
+ let prev = dummy, cur = head;
134
+ while (cur != null ) {
135
+ if (prev .val <= cur .val ) {
136
+ prev = cur;
137
+ cur = cur .next ;
138
+ continue ;
139
+ }
140
+ let p = dummy;
141
+ while (p .next .val <= cur .val ) {
142
+ p = p .next ;
143
+ }
144
+ let t = cur .next ;
145
+ cur .next = p .next ;
146
+ p .next = cur;
147
+ prev .next = t;
148
+ cur = t;
149
+ }
150
+ return dummy .next ;
151
+ };
152
+ ```
153
+
116
154
### ** ...**
117
155
118
156
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {ListNode }
11
+ */
12
+ var insertionSortList = function ( head ) {
13
+ if ( head == null || head . next == null ) return head ;
14
+ let dummy = new ListNode ( head . val , head ) ;
15
+ let prev = dummy , cur = head ;
16
+ while ( cur != null ) {
17
+ if ( prev . val <= cur . val ) {
18
+ prev = cur ;
19
+ cur = cur . next ;
20
+ continue ;
21
+ }
22
+ let p = dummy ;
23
+ while ( p . next . val <= cur . val ) {
24
+ p = p . next ;
25
+ }
26
+ let t = cur . next ;
27
+ cur . next = p . next ;
28
+ p . next = cur ;
29
+ prev . next = t ;
30
+ cur = t ;
31
+ }
32
+ return dummy . next ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments