148.排序链表

2020/12/21

# Heading

    148.排序链表 (opens new window)

    Tags: algorithms linked-list sort

    Langs: c cpp csharp golang java javascript kotlin php python python3 ruby rust scala swift typescript

    • algorithms
    • Medium (67.81%)
    • Likes: 929
    • Dislikes: -
    • Total Accepted: 128.7K
    • Total Submissions: 189.9K
    • Testcase Example: '[4,2,1,3]'

    给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表

    进阶:

    • 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?

    示例 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
    /*
     * @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 === null) return null;
    
        
    };
    // @lc code=end
    
    
    // @after-stub-for-debug-begin
    module.exports = sortList;
    // @after-stub-for-debug-end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28

    @TODO 自顶向下的归并(递归) 自底向上的归并(效率最高)