题目描述

使用插入排序对链表进行排序。
Sort a linked list using insertion sort.

示例1

输入

复制

{3,2,4}

输出

复制

{2,3,4}


// 插入排序就是不断的向一个已经排序的列表中(此处为代码中的sortedList)添加新的节点,并且保证添加节点后的列表仍然有序。
//     一开始的时候sortedList为空,需要遍历输入链表(也就是未排序链表,此处为形参head)的每一个节点,每遍历一个,sortedList加一个。
//      cur代表的就是你当前要加入sortedlist的节点。cur要插入的位置在sortedList的哪里呢?就是此处代码中node的后面。 经过这么一轮,一个节点就被加入到了sortlist。之后同理。 /**
 * 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 || head->next==nullptr) return head;
         
        //在插入时,有可能需要在链表头插入,为了方便,新建立个链表
        ListNode sortedList(0);
        ListNode *cur=head;
         
        while(cur){
            //因为cur的指向可能会改变,所以要预先存下cur的next,以备在下次循环时使用
            ListNode *next=cur->next;
             
            //node代表排序数组的当前节点
            //从前向后遍历排序数组的每一个节点,和当前未排序数组中的节点做比较           
            ListNode* node=&sortedList;
            while(node->next!=nullptr && node->next->val<cur->val) //以为第一个元素是0,所以从next开始
            {
                node=node->next;
            }
             
            cur->next=node->next;
            node->next=cur;
            cur=next;
        }
         
        return sortedList.next;
 
    }
};


leetcode5:insertion-sort-list的更多相关文章

  1. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  2. 经典排序算法 – 插入排序Insertion sort

    经典排序算法 – 插入排序Insertion sort  插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...

  3. leetcode Insertion Sort List

    题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...

  4. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  5. 9. Sort List && Insertion Sort List (链表排序总结)

    Sort List Sort a linked list in O(n log n) time using constant space complexity.                   H ...

  6. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  7. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  8. 【LeetCode OJ】Insertion Sort List

    Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...

  9. 147. Insertion Sort List

    Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...

  10. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

随机推荐

  1. springcloud学习入门

    Springcloud入门学习笔记 1. 项目初始化配置 1. 1. 新建maven工程 使用idea创建maven项目 1. 2. 在parent项目pom中导入以下依赖 <parent> ...

  2. ActiveMQ详细入门教程系列(一)

    一.什么是消息中间件 两个系统或两个客户端之间进行消息传送,利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下 ...

  3. docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled

    故障描述: [root@docker01 ~]# docker run centos docker: Error response from daemon: Get https://registry- ...

  4. MongoDB单机部署

    MongoDB单机部署 一.环境 系统:centos7.6 DB版本:mongodb-linux-x86_64-rhel62-4.2.1.tgz 官网地址:https://www.mongodb.co ...

  5. JVM(五):JVM模型与GC

    确定垃圾 引用计数(存在循环引用问题) 根可达算法 常见的垃圾回收算法 标记清除算法-位置不连续,产生碎片 拷贝算法- 没有碎片,浪费空间 标记压缩-没有碎片,效率偏低(多线程需要进行线程同步,单线程 ...

  6. Java常见的一些经典面试题(附答案解析)

    前言: 我想每个程序员比较头疼的事情都是:工作拧螺丝,面试造火箭吧.但是又必须经历这个过程,尤其是弄不清面试官问的问题,如果你准备的不是很充分,会导致面试的时候手足无措.今天这篇文章是从已工作5年的程 ...

  7. docker-compose编写示例

    docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...

  8. centos8上安装phpmyadmin5

    一,下载phpmyadmin5: 1,官网地址: https://www.phpmyadmin.net/ 2,下载 [root@yjweb source]# wget https://files.ph ...

  9. linux(centos8):用fallocate快速生成大文件

    一,fallocate的用途? 1,用途 我们有时需要用大文件来测试下载速度, 有时需要用大文件来覆盖磁盘空间, 如果在网上搜索,很多文章讲的是使用dd等工具, 事实上linux系统已经内置了生成大文 ...

  10. suse使用镜像源

    创建挂载目录,随便建个目录 mkdir /mnt/cdrom 光盘连上,挂载光盘 mount -t iso9660 /dev/sr0 /mnt/cdrom/#确保挂上了ls看看/mnt/cdrom是否 ...