• [leetcode]Insertion Sort List


    给一个链表,用插入排序...

    就是考链表的基本操作...

    开始想法很2,重新new一个链表...

    但是我明明都有链表了,我去new干嘛呢...

    其实嘛,排序就是把链表重新连下就好啦,一个一个的独立块,把next重新连下

    要是不做这题我还真不会这么去做,平时链表用的太少了T_T

    还有就是单向链表,要在当前位置插入东西的话要记录他的上一位置才行哟。。。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *insertionSortList(ListNode *head) {
            if (head == nullptr) return head;
            
            ListNode* curr = head -> next;
            head -> next = nullptr;
            
            while(curr != nullptr){
                ListNode* tmpHead = head;
                ListNode* prev = nullptr;
                ListNode* next = curr -> next;
                while(tmpHead != nullptr && tmpHead -> val <= curr -> val)
                {
                    prev = tmpHead;
                    tmpHead = tmpHead -> next;
                }
                
                if(prev != nullptr){
                    //insert
                    if(prev -> next){
                        curr -> next = prev -> next;
                        prev -> next = curr;
                    }else
                    {
                        prev -> next = curr;
                        curr -> next = nullptr;
                    }
                }else{
                    curr -> next = head;
                    head = curr;
                }
                curr = next;
            }
            return head;
        }
        
    };
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode *insertionSortList(ListNode *head) {
            if (head == nullptr) return head;
            ListNode* sort_head = head;
            ListNode* prev = nullptr;
            head = head->next;
            sort_head->next = nullptr;
            while(head) {
                prev = nullptr;
                ListNode* tmp = sort_head;
                ListNode* now = head;
                ListNode* next = head->next;
                while(tmp) {
                    if (tmp->val > now->val) break;
                    prev = tmp;
                    tmp = tmp->next;
                }
                if (prev) {
                    ListNode* next = prev->next;
                    prev->next = now;
                    now->next = next;
                } else {
                    now->next = sort_head;
                    sort_head = now;
                }
                head = next;
            }
            return sort_head;
        }
    };
  • 相关阅读:
    Vi 和 Vim
    The C Programming Language-Chapter 5 Pointers and Arrays
    C# 4.0开始,泛型接口和泛型委托都支持协变和逆变
    数据库中的锁 and java StampedLock ReadWriteLock
    NetCore and ElasticSearch 7.5
    网关项目 ReverseProxy
    异常捕获&打印异常信息
    刷新:重新发现.NET与未来
    2019 中国.NET 开发者峰会正式启动
    .NET开发者必须学习.NET Core
  • 原文地址:https://www.cnblogs.com/x1957/p/3492311.html
Copyright © 2020-2023  润新知