Leetcode Sort List

本文介绍了一种使用归并排序算法对链表进行排序的方法。通过遍历链表,将其节点存储于数组中,再利用归并排序的思想对数组中的链表节点进行排序,最后连接成有序链表。

题意:将链表排序。

思路:使用归并排序。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<ListNode*> mylist;
    ListNode* sortList(ListNode* head) {
        if(head == NULL) return head;
        ListNode* next = head;
        while(next) {
            mylist.push_back(next);
            next = next->next;
        }
        
        next = mysort(0, mylist.size() - 1);
        
        return next;
    }
    
    ListNode* mysort(int low, int high) {
        if(low == high) {
            mylist[low]->next = NULL;
            return mylist[low];
        }
        
        int mid = low + (high - low) / 2;
        
        ListNode* temp1 = mysort(low, mid); //if(temp1) cout << temp1->val << endl;
        ListNode* temp2 = mysort(mid + 1, high); //if(temp2) cout << temp2->val << endl;
        cout << endl;
        
        ListNode* myhead = new ListNode(0);
        ListNode* next = myhead;
        
        while(temp1 || temp2) {
            ListNode* tempnext = NULL;
            if(temp1 && temp2) {
                if(temp1->val < temp2->val) {
                    tempnext = temp1;
                    temp1 = temp1->next;
                    
                }
                else {
                    tempnext = temp2;
                    temp2 = temp2->next;
                }
                next->next = tempnext;
                next = tempnext;
                continue;
            }
            if(temp1 && !temp2) {
                tempnext = temp1;
                temp1 = temp1->next;
                next->next = tempnext;
                next = tempnext;
                continue;
            }
            if(!temp1 && temp2) {
                tempnext = temp2;
                temp2 = temp2->next;
                next->next = tempnext;
                next = tempnext;
                continue;
            }
        }
        //next->next = NULL;
        //show the list
        /*
        ListNode* testnext = myhead;
        while(testnext) {
            cout << testnext->val << " ";
            testnext = testnext->next;
        }
        cout << endl;
        */
        next = myhead->next;
        delete myhead;
        
        return next;
    }
};


### 使用 `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]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值