-
Notifications
You must be signed in to change notification settings - Fork 0
/
148.swift
65 lines (59 loc) · 1.97 KB
/
148.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class ListNode {
public var val: Int
public var next: ListNode?
public init() { self.val = 0; self.next = nil; }
public init(_ val: Int) { self.val = val; self.next = nil; }
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
}
class Solution {
func sortList(_ head: ListNode?) -> ListNode? {
// 链表排序
// 常数空间; O(n logn) 时间
// 中分链表,返回 合并“已排序的子链表”的结果
// 规约成 “合并已排序的链表” 问题
guard let head = head else {
return nil
}
func recurv(_ head: ListNode?, _ count: Int) -> ListNode? {
switch count {
case 0:
return nil
case 1:
return head
default:
// divide
let mid = count / 2 // index of the first of right
var llast = head!
for _ in 0..<(mid - 1) {
llast = llast.next!
}
var r = llast.next
llast.next = nil
var l = recurv(head, mid)
r = recurv(r, count - mid)
// merge
let res = ListNode(0) // dumb head
var cur = res
while l != nil, r != nil {
if l!.val < r!.val {
cur.next = l
l = l!.next
} else {
cur.next = r
r = r!.next
}
cur = cur.next!
}
cur.next = (l != nil) ? l : r
return res.next
}
}
var count = 0
var node: ListNode? = head
while node != nil {
node = node!.next
count += 1
}
return recurv(head, count)
}
}