zoukankan      html  css  js  c++  java
  • leetcode 148 链表排序的归并排序和插入排序

    ### 解题思路
    此处撰写解题思路
    
    ### 代码
    
    ```cpp
    /**
     * 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) {
            return sort_list(head, nullptr);
        }
        //排序指定区间段内的链表,左闭右开
        ListNode* sort_list(ListNode* head,ListNode* tail)
        {
            //空链表
            if(head==nullptr)
            {
                return head;
            }
            //右边边界不取,所以限定条件是==tail 如果等于了就说明只有一个元素
            if(head->next==tail)
            {
                //head后面就没了,断开和后面之前的那些联系,因为他就只有一个节点
                head->next=nullptr;
                return head;
            }
            ListNode* fast=head;
            ListNode* slow=head;
            //快慢指针 快走到头慢就是中点
            while(fast!=tail)
            {
                fast=fast->next;
                slow=slow->next;
                //fast是否到达有边界
                if(fast!=tail)
                {
                    fast=fast->next;
                }
            }
            ListNode* mid=slow;
            //归并两边 [head,mid)和[mid,null)
            return merge(sort_list(head,mid),sort_list(mid,tail));
        }
    
         ListNode* merge(ListNode* l1,ListNode* l2)
        {
            ListNode* start=new ListNode();
            ListNode* keep=new ListNode();
            //决定头 当然也可以弄个假头就不用这么判断了
            if(l1->val<l2->val)
            {
                start->val=l1->val;
                l1=l1->next;
                keep=start;
            }
            else{
                start->val=l2->val;
                l2=l2->next;
                keep=start;
            }
            while(l1!=nullptr && l2!=nullptr)
            {
                if(l1->val<l2->val)
                {
                    ListNode* newnode=new ListNode(l1->val);
                    start->next=newnode;
                    start=newnode;
                    l1=l1->next;
                }
                else{
                    ListNode* newnode=new ListNode(l2->val);
                    start->next=newnode;
                    start=newnode;
                    l2=l2->next;
                }
            }
            if(l1!=nullptr)
            {
                start->next=l1;
            }
            if(l2!=nullptr)
            {
                start->next=l2;
            }
            return keep;
        }
    };
    
        /*
            //插入
            //新链表的头 赋一个最小值
            ListNode* nohead=new ListNode(-INT_MAX);
            //index遍历原始链表
            ListNode* index=head;
            while(index!=NULL)
            {
                ListNode* index1=nohead;
                //记录遍历新链表时的前一个节点  因为插入要在前一个的后面插入
                ListNode* pre=nohead;
                while(index1!=NULL)
                {
                    if(index->val>index1->val)
                    {
                        //当前要排序的值比新链表的值大,那么新链表继续往后看,因为大的要往后插
                        pre=index1;
                        index1=index1->next;
                    }
                    else{
                        //找到插入的位置了就插入
                        ListNode* newnode=new ListNode(index->val);
                        pre->next=newnode;
                        newnode->next=index1;
                        break;//跳出去
                    }
                }
                if(index1==NULL)//到新链表的结尾了没找到比他大的 插在最后
                {
                     ListNode* newnode1=new ListNode(index->val);
                     pre->next=newnode1;
                }
                index=index->next; //切记切记,不然又死循环了。。总忘记
            }
            return nohead->next;//假头后面的就是真实的第一个了
            */
    
    
    ```

    每天进步一点点~
  • 相关阅读:
    MySQL之视图
    C# 学习笔记(二) 时间格式化字符串
    C# 学习笔记(一) Winform利用Assembly反射动态创建窗体
    puppet 4.4 System Requirements
    Linux下MySql启动时报错
    Linux Iptables
    Nginx Configure时配置
    Wireshark 使用教程
    Linux 下安装oracle数据库
    CaseFile
  • 原文地址:https://www.cnblogs.com/libin123/p/15306897.html
Copyright © 2011-2022 走看看