给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
思路:归并排序
1、通过快慢指针找到链表中点
需要确定链表的中点以进行两路归并。可以通过快慢指针的方法。快指针每次走两步,慢指针每次走一步。遍历完链表时,慢指针停留的位置就在链表的中点。
看个人习惯,我喜欢快慢指针起始位置一样
ListNode fast = head;
ListNode slow = head;
ListNode slow = head;
ListNode fast = head;
ListNode pre = slow; //前驱指针
while(fast!=null && fast.next!=null){
pre = slow;
slow = slow.next; //慢指针走一步
fast = fast.next.next; //快指针走两步
}
ListNode rightHead = slow; //链表第二部分的头节点
pre.next = null; //cut 链表
2、断链操作
public ListNode split(ListNode head,int step){
if(head==null) return null;
ListNode cur = head;
//注意这里cur.next!=null 有可能出现后半段还没到规定步长但是走完的情况
for(int i=1; i<step && cur.next!=null; i++){
cur = cur.next;
}
ListNode right = cur.next; //right为后半段链表头
cur.next = null; //切断前半段
return right; //返回后半段链表头
}
3、合并两个链表
public ListNode merge(ListNode h1, ListNode h2){
ListNode head = new ListNode(0); //新创建一个伪头节点
ListNode p = head;
while(h1!=null && h2!=null){
if(h1.val < h2.val){
p.next = h1;
h1 = h1.next;
}
else{
p.next = h2;
h2 = h2.next;
}
p = p.next;
}
//判断哪个链表还有没被比较完的值,直接把p指针指向它
if(h1!=null) p.next = h1;
if(h2!=null) p.next = h2;
return head.next; //返回排序好的链表头
}
完整的代码:
/* 1:归并排序的整体思想
* 2:找到一个链表的中间节点的方法
* 3:合并两个已排好序的链表为一个新的有序链表,和之前的LeetCode21题一样
*/
public ListNode sortList(ListNode head) {
if (head == null || head.next == null){
return head;
}
//快慢指针
ListNode fast = head;
ListNode slow = head;
ListNode pre = head;
//指针寻找链表中点
while (fast != null && fast.next != null){
pre = slow;
slow = slow.next;
fast = fast.next.next;
}
//链表第二部分的头结点,cut链表
ListNode rightHead = slow;
pre.next = null;
//递归排序 前一段链表以及排序 后一段的链表
ListNode left = sortList(head);
ListNode right = sortList(rightHead);
return merge(left,right);
}
/**
* 合并两个链表
*/
private ListNode merge(ListNode l1, ListNode l2) {
ListNode node = new ListNode(0);
ListNode p = node;
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;
}else if (l2 != null){
p.next = l2;
}
return node.next;
}
方法二:迭代
public ListNode sortList(ListNode head) {
int length = getLength(head);
ListNode dummy = new ListNode(0);
dummy.next = head;
for(int step = 1; step < length; step*=2){ //依次将链表分成1块,2块,4块...
//每次变换步长,pre指针和cur指针都初始化在链表头
ListNode pre = dummy;
ListNode cur = dummy.next;
while(cur!=null){
ListNode h1 = cur; //第一部分头 (第二次循环之后,cur为剩余部分头,不断往后把链表按照步长step分成一块一块...)
ListNode h2 = split(h1,step); //第二部分头
cur = split(h2,step); //剩余部分的头
ListNode temp = merge(h1,h2); //将一二部分排序合并
pre.next = temp; //将前面的部分与排序好的部分连接
while(pre.next!=null){
pre = pre.next; //把pre指针移动到排序好的部分的末尾
}
}
}
return dummy.next;
}
public int getLength(ListNode head){
//获取链表长度
int count = 0;
while(head!=null){
count++;
head=head.next;
}
return count;
}
public ListNode split(ListNode head,int step){
//断链操作 返回第二部分链表头
if(head==null){
return null;
}
ListNode cur = head;
for(int i=1; i<step && cur.next!=null; i++){
cur = cur.next;
}
ListNode right = cur.next;
cur.next = null; //切断连接
return right;
}
public ListNode merge(ListNode h1, ListNode h2){
//合并两个有序链表
ListNode head = new ListNode(-1);
ListNode p = head;
while(h1!=null && h2!=null){
if(h1.val < h2.val){
p.next = h1;
h1 = h1.next;
}
else{
p.next = h2;
h2 = h2.next;
}
p = p.next;
}
if(h1!=null){
p.next = h1;
}
if(h2!=null){
p.next = h2;
}
return head.next;
}