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

相关推荐
IpdataCloud2 小时前
IP查询工具的准确率怎么评估?一份可上生产的选型与验收指南
网络·人工智能·算法
生信研究猿2 小时前
leetcode 78.子集
算法·leetcode·深度优先
sycmancia2 小时前
Qt——文本编辑器中的功能交互
qt·算法
浅念-3 小时前
分治算法专题|LeetCode高频经典题目详细题解
数据结构·c++·算法·leetcode·职场和发展·排序·分治
Magic-Yuan3 小时前
算力的迷雾
人工智能·算法·机器学习
何何____3 小时前
web组第一次考核题解
算法
wayz113 小时前
Day 16:PCA主成分分析与降维
人工智能·算法·机器学习
熬夜敲代码的猫3 小时前
C++继承:让你从入门到深入
c++·算法·继承
人道领域3 小时前
【LeetCode刷题日记】239.滑动窗口最大值:单调队列解法(困难)
java·开发语言·算法