zoukankan      html  css  js  c++  java
  • leetcode 148 排序链表

    简介

    递归的算法很巧妙.

    算法思想:

    1. 判断递归终止条件
    2. 将链表划分成两部分
    3. 进行递归判断左右
    4. 将返回的两部分头结点, 进行有序合并
    5. 返回 头结点

    code

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode() : val(0), next(nullptr) {}
     *     ListNode(int x) : val(x), next(nullptr) {}
     *     ListNode(int x, ListNode *next) : val(x), next(next) {}
     * };
     */
    class Solution {
    public:
        ListNode* sortList(ListNode* head) {
            if(head == nullptr || head->next == nullptr) return head;
            // 寻找中点
            ListNode * fast = head->next; ListNode *slow = head;
            while(fast != nullptr && fast->next != nullptr) {
                slow = slow->next;
                fast = fast->next->next;
            }
            ListNode *tmp = slow->next; // 断开两段
            slow->next = nullptr;
            ListNode * left = sortList(head);
            ListNode * right = sortList(tmp);
            ListNode * h = new ListNode(0); // 虚头
            ListNode * res = h;
            while(left != nullptr && right != nullptr){
                if(left->val < right-> val) {
                    h->next = left;
                    left = left->next;
                } else {
                    h->next = right;
                    right = right->next;
                }
                h = h->next; // 进行合并, h 和 left 或 right 同步合并
            }
            h->next = left != nullptr ? left : right;
            return res->next;
        }
    };
    
    /**
     * 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;
            }
            ListNode fast = head.next, slow = head;
            while(fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            ListNode tmp = slow.next;
            slow.next = null;
            ListNode left = sortList(head);
            ListNode right = sortList(tmp);
            ListNode h = new ListNode(0);
            ListNode res = h;
            while(left != null && right != null) {
                if(left.val < right.val) {
                    h.next = left;
                    left = left.next;
                } else {
                    h.next = right;
                    right = right.next;
                }
                h = h.next;
            }
            h.next = left != null ? left : right;
            return res.next;
        }
    }
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    纸牌游戏----小猫钓鱼
    数据结构-----栈
    浅谈队列
    排序算法实例
    排序算法之------快速排序
    排序算法之----冒泡排序
    Visual Studio-基本使用
    C++-GUID from string
    OS-Windows CMD生成文件夹目录结构
    OS-Windows10 DownLoad
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14866970.html
Copyright © 2011-2022 走看看