zoukankan      html  css  js  c++  java
  • [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;
        }
    };
    
    欢迎转载,转载请注明出处!
  • 相关阅读:
    如何在typescript中引入jquery
    Project 'com.cooldatasoft:spring-boot-starter-parent:2.3.10.RELEASE' not found
    FTP服务器需要开几个端口
    c# 创建 windows 托盘图标及上下文菜单
    postgresql 查询表注释 和 字段注释
    idea 使用技巧识记
    c# 基元数据类型占用字节数
    proxifier 配合 fiddler 网络监控 使用方法备忘
    c# 反射备忘
    Delphi IdFTP[3] TIdFTP.Connect、IdFTP.ReadTimeout
  • 原文地址:https://www.cnblogs.com/huihao/p/15435040.html
Copyright © 2011-2022 走看看