zoukankan      html  css  js  c++  java
  • [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;
        }
    };
  • 相关阅读:
    ABP之模块分析
    AutoMapper之ABP项目中的使用介绍
    Castle Windsor常用介绍以及其在ABP项目的应用介绍
    EasyUI实战经验总结,给有需要的人
    无法发送具有此谓词类型的内容正文
    ADO.NET 新特性之SqlBulkCopy
    SVN无法Cleanup
    Mac使用操作
    Mac下的Mysql无法登陆的问题
    mac 终端 常用命令
  • 原文地址:https://www.cnblogs.com/x1957/p/3492311.html
Copyright © 2011-2022 走看看