排序链表
给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
/**
* 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}
*/
const sortList = (head) => {
const res = []
while (head) {
res.push(head.val)
head = head.next
}
res.sort((x, y) => y - x)
let cur = null
res.forEach(i => {
cur = new ListNode(i, cur)
})
return cur
};
以自己现在的努力程度,还没有资格和别人拼天赋

浙公网安备 33010602011771号