Skip to content

Latest commit

 

History

History
executable file
·
38 lines (26 loc) · 556 Bytes

File metadata and controls

executable file
·
38 lines (26 loc) · 556 Bytes

148. 排序链表

https://leetcode-cn.com/problems/sort-list

Solution

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
}

Tip

  • 排序重造