[148] 排序链表

This commit is contained in:
Wen 2023-08-14 14:14:30 +08:00
parent 5833ea04f0
commit 270e53db52
1 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/*
* @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 || !head.next) {
return head;
}
let slow = head,
fast = head.next;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
const tmp = slow.next;
slow.next = null;
let left = sortList(head);
let right = sortList(tmp);
let h = new ListNode(0);
const ret = h;
while (left && right) {
if (left.val < right.val) {
h.next = left;
left = left.next;
} else {
h.next = right;
right = right.next;
}
h = h.next;
}
h.next = left == null ? right : left;
return ret.next;
};
// @lc code=end