题目描述
给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
示例 1:
输入:head = [4,2,1,3] 
输出:[1,2,3,4]
示例 2:
输入:head = [-1,5,3,4,0] 
输出:[-1,0,3,4,5]
示例 3:
输入:head = [] 
输出:[]
提示:
- 链表中节点的数目在范围 [0, 5 * 104] 内
 - -105 <= Node.val <= 105
 
进阶:你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
个人解法
Javascript
/** @lc app=leetcode.cn id=148 lang=javascript** [148] 排序链表*/// @lc code=start/*** 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) {return null;}let myArr = [];let temp = head;while (temp) {myArr.push(temp);temp = temp.next;}myArr.sort((a, b) => {return a.val - b.val;})let res = myArr[0];let nowTemp = res;for (let i = 1; i < myArr.length; i++) {nowTemp.next = myArr[i];nowTemp = myArr[i];}nowTemp.next = null;return res;};// @lc code=end
Java
其他解法
Java
Javascript
归并排序
const merge = (head1, head2) => {const dummyHead = new ListNode(0);let temp = dummyHead, temp1 = head1, temp2 = head2;while (temp1 !== null && temp2 !== null) {if (temp1.val <= temp2.val) {temp.next = temp1;temp1 = temp1.next;} else {temp.next = temp2;temp2 = temp2.next;}temp = temp.next;}if (temp1 !== null) {temp.next = temp1;} else if (temp2 !== null) {temp.next = temp2;}return dummyHead.next;}const toSortList = (head, tail) => {if (head === null) {return head;}if (head.next === tail) {head.next = null;return head;}let slow = head, fast = head;while (fast !== tail) {slow = slow.next;fast = fast.next;if (fast !== tail) {fast = fast.next;}}const mid = slow;return merge(toSortList(head, mid), toSortList(mid, tail));}var sortList = function(head) {return toSortList(head, null);};
归并排序2
const merge = (head1, head2) => {const dummyHead = new ListNode(0);let temp = dummyHead, temp1 = head1, temp2 = head2;while (temp1 !== null && temp2 !== null) {if (temp1.val <= temp2.val) {temp.next = temp1;temp1 = temp1.next;} else {temp.next = temp2;temp2 = temp2.next;}temp = temp.next;}if (temp1 !== null) {temp.next = temp1;} else if (temp2 !== null) {temp.next = temp2;}return dummyHead.next;}var sortList = function(head) {if (head === null) {return head;}let length = 0;let node = head;while (node !== null) {length++;node = node.next;}const dummyHead = new ListNode(0, head);for (let subLength = 1; subLength < length; subLength <<= 1) {let prev = dummyHead, curr = dummyHead.next;while (curr !== null) {let head1 = curr;for (let i = 1; i < subLength && curr.next !== null; i++) {curr = curr.next;}let head2 = curr.next;curr.next = null;curr = head2;for (let i = 1; i < subLength && curr != null && curr.next !== null; i++) {curr = curr.next;}let next = null;if (curr !== null) {next = curr.next;curr.next = null;}const merged = merge(head1, head2);prev.next = merged;while (prev.next !== null) {prev = prev.next;}curr = next;}}return dummyHead.next;};
