2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_148.java 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2021-02-02 14:16 . bytedance
package com.hit.basmath.interview.top_interview_questions.hard_collection.linked_list;
import com.hit.common.ListNode;
/**
* 148. 排序链表
* <p>
* 给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
* <p>
* 进阶:
* <p>
* 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
* <p>
* 示例 1:
* <p>
* 输入:head = [4,2,1,3]
* 输出:[1,2,3,4]
* <p>
* 示例 2:
* <p>
* <p>
* 输入:head = [-1,5,3,4,0]
* 输出:[-1,0,3,4,5]
* <p>
* 示例 3:
* <p>
* 输入:head = []
* 输出:[]
* <p>
* 提示:
* <p>
* 链表中节点的数目在范围 [0, 5 * 10^4] 内
* -10^5 <= Node.val <= 10^5
*/
public class _148 {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
// step 1. cut the list to two halves
ListNode prev = null, slow = head, fast = head;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
// step 2. sort each half
ListNode l1 = sortList(head);
ListNode l2 = sortList(slow);
// step 3. merge l1 and l2
return merge(l1, l2);
}
private ListNode merge(ListNode l1, ListNode l2) {
ListNode root = new ListNode(0), p = root;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null) p.next = l1;
if (l2 != null) p.next = l2;
return root.next;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助