zoukankan      html  css  js  c++  java
  • 148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity.

    Example 1:

    Input: 4->2->1->3
    Output: 1->2->3->4
    

    Example 2:

    Input: -1->5->3->4->0
    Output: -1->0->3->4->5
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode sortList(ListNode head) {
            if (head == null || head.next == null)return head;
    
            final ListNode middle = findMiddle(head);
            final ListNode head2 = middle.next;
            middle.next = null; // 断开
    
            final ListNode l1 = sortList(head);  // 前半段排序
            final ListNode l2 = sortList(head2);  // 后半段排序
            return mergeTwoLists(l1, l2);
        }
    
        // Merge Two Sorted Lists
        private static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode dummy = new ListNode(-1);
            for (ListNode p = dummy; l1 != null || l2 != null; p = p.next) {
                int val1 = l1 == null ? Integer.MAX_VALUE : l1.val;
                int val2 = l2 == null ? Integer.MAX_VALUE : l2.val;
                if (val1 <= val2) {
                    p.next = l1;
                    l1 = l1.next;
                } else {
                    p.next = l2;
                    l2 = l2.next;
                }
            }
            return dummy.next;
        }
        // 快慢指针找到中间节点
        private static ListNode findMiddle(ListNode head) {
            if (head == null) return null;
    
            ListNode slow = head;
            ListNode fast = head.next;
    
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            return slow;
        }
    }

    这种答案就是让人看着非常的舒适,有一种为什么他妈的这么好的感觉,嗯

    上面是2019春写的,当时批话真的多还菜,当然现在也没改变多少--2019冬

    class Solution {
        public ListNode sortList(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode mid = findmid(head);
            ListNode right = sortList(mid.next);
            mid.next = null;
            ListNode left = sortList(head);
            return merge(left, right);
        }
        public ListNode findmid(ListNode head){
            ListNode slow = head, fast = head.next;
            while(fast != null && fast.next != null){
                slow = slow.next;
                fast = fast.next.next;
            }
            return slow;
        }
        public ListNode merge(ListNode l1, ListNode l2){
            ListNode dummy = new ListNode(-1);
            ListNode tail = dummy;
            while(l1 != null && l2 != null){
                if(l1.val < l2.val){
                    tail.next = l1;
                    l1 = l1.next;
                }
                else{
                    tail.next = l2;
                    l2 = l2.next;
                }
                tail = tail.next;
            }
            tail.next = l1 != null ? l1 : l2;
            return dummy.next;
        }
    }
  • 相关阅读:
    IDEA 2021.1 推出语音、视频功能,边写代码边聊天
    HTML5实现首页动态视频背景
    前端项目自动化构建工具——Webpack入门教程
    JavaScript多线程及事件循环机制
    SVN迁移至Git,保留commit提交记录
    Windows平台下搭建自己的Git服务器
    SVN服务器搭建、备份及多服务器同步方案(Windows)
    全图文分析:如何利用Google的protobuf,来思考、设计、实现自己的RPC框架
    注册表修改右键菜单的说明
    powershell换行输出,换行输入命令,多行命令的执行
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10541651.html
Copyright © 2011-2022 走看看