代码拉取完成,页面将自动刷新
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。