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

相关推荐
得物技术1 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六5 小时前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术5 小时前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize6 小时前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考19 小时前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队1 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法
To_OC2 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC2 天前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode
BadBadBad__AK2 天前
线段树维护区间 k 次方和
c++·数学·算法·stl