LeetCode:148排序链表

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }

        //计算总长度
        int length = 0;
        ListNode node = head;
        while(node != null){
            length++;
            node = node.next;
        }

        ListNode dummy = new ListNode(0);
        dummy.next = head;

        //步长从1开始,依次翻倍
        for(int subLength = 1; subLength < length; subLength *= 2){
            ListNode prev = dummy;
            ListNode curr = dummy.next;
            
            while(curr != null){
                //切第一段
                ListNode head1 = curr;
                for(int i = 1; i < subLength && curr.next != null; i++){
                    curr = curr.next;
                }
                //切第二段
                ListNode head2 = curr.next;
                curr.next = null;
                curr = head2;

                for(int i = 1; i < subLength && curr != null && curr.next != null; i++){
                    curr = curr.next;
                }

                //找出下一波要切的起点
                ListNode next = null;
                if(curr != null){
                    next = curr.next;
                    curr.next = null;
                }

                //合并
                ListNode merged = merge(head1,head2);
                prev.next = merged;

                while(prev.next != null){
                    prev = prev.next;
                }

                curr = next;
            }
        }
        return dummy.next;
    }
    //合并两个有序链表
    private ListNode merge(ListNode l1, ListNode l2){
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                curr.next = l1;
                l1 = l1.next;
            }else{
                curr.next = l2;
                l2 = l2.next;
            }
            curr = curr.next;
        }
        curr.next = l1 != null ? l1 :l2;
        return dummy.next;
    }
}

1.遍历链表的长度

2.步长从1开始,每次*2

3.切出第一段和第二段用于排序

4.curr走到当前块的末尾

5.next找到下一步的起点

6.合并

7.将合并好的新块与数组连接起来

8.prev指针走到刚合并块的末尾

9.curr到下一波起点

相关推荐
xieliyu.9 分钟前
Java算法精讲:双指针(三)
java·开发语言·算法
一条小锦吕*31 分钟前
基于Spring Boot + 数据可视化 + 协同过滤算法的推荐系统设计与实现(源码+论文+部署全讲解)
spring boot·算法·信息可视化
如竟没有火炬2 小时前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
8Qi83 小时前
LeetCode 1143 & 718:最长公共子序列 / 最长重复子数组
算法·leetcode·职场和发展·动态规划
绿算技术3 小时前
万卡推理集群存储选型分析:从核心架构到应用视角
大数据·科技·算法·架构
想吃火锅10054 小时前
【leetcode】1.两数之和js版
javascript·算法·leetcode
net3m335 小时前
一阶软件低通滤波器算法
人工智能·算法
水木流年追梦5 小时前
大模型入门-大模型优化方法12-YaRN 长文本外推技术
人工智能·分布式·算法·正则表达式·prompt
J-Tony116 小时前
【JVM】三色标记法
java·jvm·算法