题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
function sortInList( head ) {
// write code here
if( head.next === null) return head
let arr = []
let p = head
while(p){
arr.push(p)
p = p.next
}
arr.sort((a ,b) => { return a.val - b.val })
for(let i=0; i<arr.length; i++)
{
if(i === arr.length-1)
{
arr[i].next = null
}
else
{
arr[i].next = arr[i+1]
}
}
return arr[0]
}
// write code here
if( head.next === null) return head
let arr = []
let p = head
while(p){
arr.push(p)
p = p.next
}
arr.sort((a ,b) => { return a.val - b.val })
for(let i=0; i<arr.length; i++)
{
if(i === arr.length-1)
{
arr[i].next = null
}
else
{
arr[i].next = arr[i+1]
}
}
return arr[0]
}