题解 | #单链表的排序#

单链表的排序

http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08

方法1 开辟新数组

  • 空间复杂度:O(n) ---开辟了一个大小为n的数组储存元素
  • 时间复杂度:O(nlogn) ---对数组里的元素进行排序
  • 根据排序好的数组新建链表
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    ListNode* sortInList(ListNode* head) {
        // write code here
        if(head == nullptr) return head;
        vector<int> s;
        while(head){
            s.push_back(head->val);
            head = head->next;
        }
        sort(s.begin(), s.end());
        ListNode* cur = new ListNode(-1);
        head = cur;
        for(int i = 0; i < s.size(); i++){
            ListNode* node = new ListNode(s[i]);
            cur->next = node;
            cur = cur->next;
        }
        return head->next;
        
    }
};

方法二 归并排序

用快慢指针找到中间节点一分为二,然后归并(相当于树的后序遍历),归并操作为将两条链表合并为一条有序链表

  • 空间复杂度:O(1) ---原地排序
  • 时间复杂度:O(nlogn) ---归并的复杂度

class Solution {
public:
    ListNode* sortInList(ListNode* head) {
        // write code here
        if(head == nullptr || head->next ==nullptr) return head;
        //fast开始时位于2,slow位于1,这样当fast走到空时,slow:奇数个节点找到中点,偶数个节点找到中心左边的节点。
        ListNode* fast = head->next;
        ListNode* slow = head; 
        while(fast != nullptr && fast->next != nullptr){
            fast = fast->next->next;
            slow = slow->next;
        }
        ListNode* tmp = slow->next;
        slow->next = nullptr;
        //归并排序:相当于树的后序遍历,先处理两边,再处理中间(左右中)
        ListNode* left =  sortInList(head);
        ListNode* right = sortInList(tmp);
        //将两条链表升序排序
        ListNode* h = new ListNode(-1);
        ListNode* cur = h;
        while(left && right){
            if(left->val < right->val){
                cur->next = left;
                left = left->next;
            }else{
                cur->next = right;
                right = right->next;
            }
            cur = cur->next;
        }
        if(left) cur->next = left;
        if(right) cur->next = right;
        return h->next;
    }
};

全部评论

相关推荐

06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
第一份工作能做外包吗?
点赞 评论 收藏
分享
05-21 15:47
门头沟学院 Java
浪漫主义的虹夏:项目有亮点吗,第一个不是纯玩具项目吗,项目亮点里类似ThreadLocal,Redis储存说难听点是花几十分钟绝大部分人都能学会,第二个轮子项目也没体现出设计和技术,想实习先沉淀,好高骛远的自嗨只会害了自己
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务