File tree 2 files changed +111
-0
lines changed
lcof2/剑指 Offer II 077. 链表排序
2 files changed +111
-0
lines changed Original file line number Diff line number Diff line change @@ -377,6 +377,64 @@ public class Solution {
377
377
}
378
378
```
379
379
380
+ #### Swift
381
+
382
+ ``` swift
383
+
384
+ /** class ListNode {
385
+ * var val: Int
386
+ * var next: ListNode?
387
+ * init() { self.val = 0; self.next = nil }
388
+ * init(_ val: Int) { self.val = val; self.next = nil }
389
+ * init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next }
390
+ * }
391
+ */
392
+
393
+ class Solution {
394
+ func sortList (_ head : ListNode? ) -> ListNode? {
395
+ guard let head = head, head.next != nil else {
396
+ return head
397
+ }
398
+
399
+ var slow: ListNode? = head
400
+ var fast: ListNode? = head.next
401
+
402
+ while fast != nil && fast? .next != nil {
403
+ slow = slow? .next
404
+ fast = fast? .next ? .next
405
+ }
406
+
407
+ let mid = slow? .next
408
+ slow? .next = nil
409
+
410
+ let left = sortList (head)
411
+ let right = sortList (mid)
412
+
413
+ return merge (left, right)
414
+ }
415
+
416
+ private func merge (_ l1 : ListNode? , _ l2 : ListNode? ) -> ListNode? {
417
+ let dummy = ListNode ()
418
+ var cur = dummy
419
+ var l1 = l1, l2 = l2
420
+
421
+ while let node1 = l1, let node2 = l2 {
422
+ if node1.val <= node2.val {
423
+ cur.next = node1
424
+ l1 = node1.next
425
+ } else {
426
+ cur.next = node2
427
+ l2 = node2.next
428
+ }
429
+ cur = cur.next !
430
+ }
431
+
432
+ cur.next = l1 ?? l2
433
+ return dummy.next
434
+ }
435
+ }
436
+ ```
437
+
380
438
<!-- tabs: end -->
381
439
382
440
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+
2
+ /** class ListNode {
3
+ * var val: Int
4
+ * var next: ListNode?
5
+ * init() { self.val = 0; self.next = nil }
6
+ * init(_ val: Int) { self.val = val; self.next = nil }
7
+ * init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next }
8
+ * }
9
+ */
10
+
11
+ class Solution {
12
+ func sortList( _ head: ListNode ? ) -> ListNode ? {
13
+ guard let head = head, head. next != nil else {
14
+ return head
15
+ }
16
+
17
+ var slow : ListNode ? = head
18
+ var fast : ListNode ? = head. next
19
+
20
+ while fast != nil && fast? . next != nil {
21
+ slow = slow? . next
22
+ fast = fast? . next? . next
23
+ }
24
+
25
+ let mid = slow? . next
26
+ slow? . next = nil
27
+
28
+ let left = sortList ( head)
29
+ let right = sortList ( mid)
30
+
31
+ return merge ( left, right)
32
+ }
33
+
34
+ private func merge( _ l1: ListNode ? , _ l2: ListNode ? ) -> ListNode ? {
35
+ let dummy = ListNode ( )
36
+ var cur = dummy
37
+ var l1 = l1, l2 = l2
38
+
39
+ while let node1 = l1, let node2 = l2 {
40
+ if node1. val <= node2. val {
41
+ cur. next = node1
42
+ l1 = node1. next
43
+ } else {
44
+ cur. next = node2
45
+ l2 = node2. next
46
+ }
47
+ cur = cur. next!
48
+ }
49
+
50
+ cur. next = l1 ?? l2
51
+ return dummy. next
52
+ }
53
+ }
You can’t perform that action at this time.
0 commit comments