• [LeetCode] 148. Sort List


    [LeetCode] 148. Sort List

    题目

    Given the head of a linked list, return the list after sorting it in ascending order.

    Example 1:

    Input: head = [4,2,1,3]
    Output: [1,2,3,4]
    

    思路

    归并排序

    1. 自顶向下归并,可以用快慢指针法找中点。

    2. 自底向下归并,这样用的空间少。

    image

    代码

    /**
     * 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) {
                return head;
            }
            int len = 0;
            ListNode* node = head;
            while (node != nullptr) {
                len++;
                node = node->next;
            }
            ListNode* dummyHead = new ListNode(0, head);
            for (int sub = 1; sub < len; sub <<= 1) {
                ListNode* prev = dummyHead, *curr = dummyHead->next;
                while (curr != nullptr) {
                    ListNode* head1 = curr;
                    for (int i = 1; i < sub && curr->next != nullptr; i++) {
                        curr = curr->next;
                    }
                    ListNode* head2 = curr->next;
                    curr->next = nullptr;
                    curr = head2;
                    for (int i = 1; i < sub && curr != nullptr && curr->next != nullptr; i++) {
                        curr = curr->next;
                    }
                    ListNode* next = nullptr;
                    if (curr != nullptr) {
                        next = curr->next;
                        curr->next = nullptr;
                    }
                    ListNode* merged = merge(head1, head2);
                    prev->next = merged;
                    while (prev->next != nullptr) {
                        prev = prev->next;
                    }
                    curr = next;
                }
            }
            return dummyHead->next;
    
        }
            ListNode* merge(ListNode* head1, ListNode* head2) {
            ListNode* dummyHead = new ListNode(0);
            ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
            while (temp1 != nullptr && temp2 != nullptr) {
                if (temp1->val <= temp2->val) {
                    temp->next = temp1;
                    temp1 = temp1->next;
                } else {
                    temp->next = temp2;
                    temp2 = temp2->next;
                }
                temp = temp->next;
            }
            if (temp1 != nullptr) {
                temp->next = temp1;
            } else if (temp2 != nullptr) {
                temp->next = temp2;
            }
            return dummyHead->next;
        }
    };
    
    欢迎转载,转载请注明出处!
  • 相关阅读:
    Error Domain=com.google.greenhouse Code=-102
    给分类(Category)添加属性
    Eclipse 4.4(luna) 安装Veloeclipse 2.0.8时报错的问题
    MAVEN常用命令
    Redis之七种武器
    Redis系统性介绍
    Nginx、LVS及HAProxy负载均衡软件的优缺点详解
    Redis介绍以及安装(Linux)
    MYSQL + MHA +keepalive + VIP安装配置(三)--keepalived安装配置
    MYSQL + MHA +keepalive + VIP安装配置(二)--MHA的配置
  • 原文地址:https://www.cnblogs.com/huihao/p/15435040.html
Copyright © 2020-2023  润新知