File tree 3 files changed +130
-0
lines changed
solution/0100-0199/0148.Sort List
3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change @@ -233,6 +233,51 @@ public class Solution {
233
233
}
234
234
```
235
235
236
+ ### ** TypeScript**
237
+
238
+ ``` ts
239
+ /**
240
+ * Definition for singly-linked list.
241
+ * class ListNode {
242
+ * val: number
243
+ * next: ListNode | null
244
+ * constructor(val?: number, next?: ListNode | null) {
245
+ * this.val = (val===undefined ? 0 : val)
246
+ * this.next = (next===undefined ? null : next)
247
+ * }
248
+ * }
249
+ */
250
+
251
+ function sortList(head : ListNode | null ): ListNode | null {
252
+ if (head == null || head .next == null ) return head ;
253
+ // 快慢指针定位中点
254
+ let slow: ListNode = head , fast: ListNode = head .next ;
255
+ while (fast != null && fast .next != null ) {
256
+ slow = slow .next ;
257
+ fast = fast .next .next ;
258
+ }
259
+ // 归并排序
260
+ let mid: ListNode = slow .next ;
261
+ slow .next = null ;
262
+ let l1: ListNode = sortList (head );
263
+ let l2: ListNode = sortList (mid );
264
+ let dummy: ListNode = new ListNode ();
265
+ let cur: ListNode = dummy ;
266
+ while (l1 != null && l2 != null ) {
267
+ if (l1 .val <= l2 .val ) {
268
+ cur .next = l1 ;
269
+ l1 = l1 .next ;
270
+ } else {
271
+ cur .next = l2 ;
272
+ l2 = l2 .next ;
273
+ }
274
+ cur = cur .next ;
275
+ }
276
+ cur .next = l1 == null ? l2 : l1 ;
277
+ return dummy .next ;
278
+ };
279
+ ```
280
+
236
281
### ** ...**
237
282
238
283
```
Original file line number Diff line number Diff line change @@ -217,6 +217,51 @@ public class Solution {
217
217
}
218
218
```
219
219
220
+ ### ** TypeScript**
221
+
222
+ ``` ts
223
+ /**
224
+ * Definition for singly-linked list.
225
+ * class ListNode {
226
+ * val: number
227
+ * next: ListNode | null
228
+ * constructor(val?: number, next?: ListNode | null) {
229
+ * this.val = (val===undefined ? 0 : val)
230
+ * this.next = (next===undefined ? null : next)
231
+ * }
232
+ * }
233
+ */
234
+
235
+ function sortList(head : ListNode | null ): ListNode | null {
236
+ if (head == null || head .next == null ) return head ;
237
+ // 快慢指针定位中点
238
+ let slow: ListNode = head , fast: ListNode = head .next ;
239
+ while (fast != null && fast .next != null ) {
240
+ slow = slow .next ;
241
+ fast = fast .next .next ;
242
+ }
243
+ // 归并排序
244
+ let mid: ListNode = slow .next ;
245
+ slow .next = null ;
246
+ let l1: ListNode = sortList (head );
247
+ let l2: ListNode = sortList (mid );
248
+ let dummy: ListNode = new ListNode ();
249
+ let cur: ListNode = dummy ;
250
+ while (l1 != null && l2 != null ) {
251
+ if (l1 .val <= l2 .val ) {
252
+ cur .next = l1 ;
253
+ l1 = l1 .next ;
254
+ } else {
255
+ cur .next = l2 ;
256
+ l2 = l2 .next ;
257
+ }
258
+ cur = cur .next ;
259
+ }
260
+ cur .next = l1 == null ? l2 : l1 ;
261
+ return dummy .next ;
262
+ };
263
+ ```
264
+
220
265
### ** ...**
221
266
222
267
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
+ * }
11
+ */
12
+
13
+ function sortList ( head : ListNode | null ) : ListNode | null {
14
+ if ( head == null || head . next == null ) return head ;
15
+ // 快慢指针定位中点
16
+ let slow : ListNode = head , fast : ListNode = head . next ;
17
+ while ( fast != null && fast . next != null ) {
18
+ slow = slow . next ;
19
+ fast = fast . next . next ;
20
+ }
21
+ // 归并排序
22
+ let mid : ListNode = slow . next ;
23
+ slow . next = null ;
24
+ let l1 : ListNode = sortList ( head ) ;
25
+ let l2 : ListNode = sortList ( mid ) ;
26
+ let dummy : ListNode = new ListNode ( ) ;
27
+ let cur : ListNode = dummy ;
28
+ while ( l1 != null && l2 != null ) {
29
+ if ( l1 . val <= l2 . val ) {
30
+ cur . next = l1 ;
31
+ l1 = l1 . next ;
32
+ } else {
33
+ cur . next = l2 ;
34
+ l2 = l2 . next ;
35
+ }
36
+ cur = cur . next ;
37
+ }
38
+ cur . next = l1 == null ? l2 : l1 ;
39
+ return dummy . next ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments