https://leetcode-cn.com/problems/sort-list
Swift
func sortList(_ head: ListNode?) -> ListNode? {
guard let h = head else {
return head
}
var numbers = [Int]()
var current: ListNode? = h
while current != nil {
numbers.append(current!.val)
current = current?.next
}
numbers.sort()
current = h
for element in numbers {
current?.val = element
current = current?.next
}
return h
}
- 排序重造