leetcode Sort List

探讨了在O(ologn)时间复杂度下对链表进行排序的方法,对比了快速排序和归并排序在链表场景下的应用,最终采用链表归并排序实现更优效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没想到什么好办法,要将O(ologn)时间里完成,我先遍历一遍将链表存储到数组里,再对数组进行排序,排序方法写了快排和归并的,过是能过,但是时间要140ms,最快的就是直接对链表进行归并排序,第一次写比较难受,用快慢指针来分成两个链表,具体思想还是和数组的归并排序一样。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

void merge(int A[],int L1,int R1,int L2,int R2){
    int i = L1, j = R2; // i指向A[L1] j指向A[L2]
    int temp[100000], Index = 0;
    while (i <= R1 && j<=R2) {
        if (A[i] <= A[j])
            temp[Index++] = A[i++];
        else
            temp[Index++] = A[j++];
    }
    while (i <= R1) temp[Index++] = A[i++];
    while (j <= R2) temp[Index++] = A[j++];
    for (int i =0; i<Index; i++)
        A[L1 + i] = temp[i];
}

void merge_sort(int A[],int left,int right){
    if (left < right){
        int mid = (left+right) / 2;
        merge_sort(A, left, mid);
        merge_sort(A, mid+1, right);
        merge(A, left, mid,mid+1,right);
    }
}
int Partition(int A[],int left,int right){
    int temp = A[left];
    while (left < right) {
        while (left < right && A[right] > temp)
            right--;
        A[left] = A[right];
        while (left <right && A[left] <= temp)
            left++;
        A[right] = A[left];
    }
    A[left] = temp;
    return left;
}

void quick_sort(int A[],int left,int right){
    if (left < right) {
        int pos = Partition(A, left, right);
        quick_sort(A, left, pos-1);
        quick_sort(A, pos+1, right);
    }
}


struct ListNode* sortList(struct ListNode* head) {
    if(!head || !head->next)
        return head;
    int data[100000];
    int index = 0; // the length of the list
    struct ListNode *current = head;
    while(current){
        data[index++] = current->val;
        current = current->next;
    }
    quick_sort(data,0,index-1);
    current = head;
    index = 0;
    while(current){
        current->val = data[index++];
        current = current->next;
    }
    return head;
}

 

### 使用 `sort` 函数在 LeetCode 上实现数组或列表排序 #### C++ 实现 在 C++ 中,可以使用标准库中的 `std::sort()` 来对向量 (vector) 进行排序。此函数采用迭代器作为参数来指定要排序的范围。 ```cpp class Solution { public: void sortVector(std::vector<int>& vec) { std::sort(vec.begin(), vec.end()); // 对整个vec升序排列 } }; ``` 上述代码展示了如何定义一个名为 `Solution` 的类,并在其内部创建了一个成员函数 `sortVector`,该函数接收一个整数类型的引用向量并对其进行排序[^1]。 为了降序排列,则需提供自定义比较谓词: ```cpp bool descending(int a, int b){ return a > b; } //... std::sort(vec.begin(), vec.end(), descending); ``` 这里通过传递第三个参数给 `std::sort()`, 即实现了按特定规则(这里是大于号表示的大于关系)来进行排序操作。 #### Python 实现 Python 提供了内置的方法 `.sort()` 和全局函数 `sorted()` 可用于原地修改对象或返回新的已排序序列副本。下面的例子展示怎样在一个解决方案类里调用这些功能: ```python from typing import List class Solution: def sortList(self, lst: List[int]): lst.sort() # 原位排序lst @staticmethod def get_sorted_copy(lst: List[int]) -> List[int]: return sorted(lst) # 返回一个新的有序列表而不改变原始数据 ``` 这段代码同样构建了一个叫做 `Solution` 的类,在其中包含了两种不同的方式来处理输入列表 `lst`:一种是在原有基础上直接进行排序;另一种则是生成一份经过排序的新列表副本[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值