尼采般地抒情

尼采般地抒情

尼采般地抒情

音乐盒

站点信息

文章总数目: 316
已运行时间: 1570

148. 排序链表

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var sortList = function(head) {
    if (head === null) return head
    let arr = []
    while (head !== null) {
        arr.push(head.val)
        head = head.next
    }
    let result = arr.sort((a, b) => {return a - b})
    let result_head = new ListNode(result[0], null)
    let test = result_head
    result.forEach((data, index) => {
        if (index !== 0) {
            let temp = new ListNode(data,null)
            test.next = temp
            test = temp
        }
    })
    return result_head
};

评论区