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到下一波起点

相关推荐
ZhengEnCi5 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
秋天的怀念_David5 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi6 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6186 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang6 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
2601_970703838 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
心中有你02148 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
ZhengEnCi9 小时前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
Flynn Liang9 小时前
RAG 答非所问/幻觉怎么破?Spring AI QuestionAnswerAdvisor 召回优化实战(TopK/重排/混合检索)
人工智能·算法·spring·rag·幻觉·springai
liminla!9 小时前
SQL 从入门到进阶:语法速查与实战练习(完整优化版)
数据库·sql·算法