描述:
Sort a linked list in O(n log n) time using constant space complexity.
URL
https://leetcode.com/problems/sort-list/description/
解释
如果时间复杂度是nlogn,则需要使用快速排序或者归并排序的思想,下面给出两种算法
代码
//归并排序
/**
* Definition for singly-linked list.
**/
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode sortList(ListNode head){
return mergeSort(head);
}
private ListNode mergeSort(ListNode head){
if(head==null || head.next==null){
return head;
}
//利用快慢指针找到中间的节点
ListNode slow = head;
ListNode fast = head;
while(fast!=null && fast.next!=null
&& fast.next.next!=null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode mid = slow.next;
slow.next = null;
//分别排序前半段和后半段
ListNode left = mergeSort(head);
ListNode right = mergeSort(mid);
return merge(left,right);
}
//merge两段链表
private ListNode merge(ListNode left,ListNode right) {
ListNode pre = new ListNode(0);
ListNode rear = pre;
while(left!=null && right!=null){
if(left.val<right.val){
rear.next = left;
rear = rear.next;
left = left.next;
}else{
rear.next = right;
rear = rear.next;
right = right.next;
}
}
if(left!=null){
rear.next = left;
}
if(right!=null){
rear.next = right;
}
return pre.next;
}
//快速排序算法
/**
* Definition for singly-linked list.
**/
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
//定义一个结构,每一次分段之后将数据存储在这个结构中
static class Partion{
ListNode small = new ListNode(-1);
ListNode equal = new ListNode(-1);
ListNode large = new ListNode(-1);
}
public ListNode sortList(ListNode head) {
return quickSort(head);
}
//快速排序
private ListNode quickSort(ListNode head){
if(head == null) return head;
Partion p = partion(head);
//下面代码将small,equal,large 链接在一起
ListNode nh= new ListNode(-1),tmp=null;
if(p.small != null){
nh.next = quickSort(p.small);
}
tmp = nh;
while(tmp.next!=null){
tmp = tmp.next;
}
tmp.next = p.equal;
while(tmp.next!=null){
tmp = tmp.next;
}
tmp.next = quickSort(p.large);
return nh.next;
}
//分段,与key比较,分别放入small,equal,large三个链表
private Partion partion(ListNode head) {
Partion p = new Partion();
int key = head.val;
ListNode next;
while(head != null) {
next = head.next;
if(head.val<key){
head.next = p.small.next;
p.small.next = head;
}else if(head.val > key){
head.next = p.large.next;
p.large.next = head;
}else{
head.next = p.equal.next;
p.equal.next = head;
}
head = next;
}
p.small = p.small.next;
p.large = p.large.next;
p.equal = p.equal.next;
return p;
}