我的js算法爬坑之旅- 排序链表

64 阅读1分钟

第四十五天:力扣第148题,排序链表

地址:leetcode-cn.com/problems/so…

思路:不太会,只能偷鸡,转为数组做再转为链表

var sortList = function(head) {
  let res = [];
  let p = head;
  while(p)
  {
    res.push(p.val);
    p = p.next;
  }
  p = head;
  res.sort((a,b) => a-b);
  for(let i = 0; i < res.length; i++)
  {
    head.val = res[i];
    head = head.next;
  }
  return p;
}

执行用时:148 ms, 在所有 JavaScript 提交中击败了49.11%的用户

内存消耗:51.6 MB, 在所有 JavaScript 提交中击败了35.02%的用户