leetcode5:insertion-sort-list
题目描述
输出
{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的更多相关文章
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- 经典排序算法 – 插入排序Insertion sort
经典排序算法 – 插入排序Insertion sort 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕. 插入排序方法分直接插入排序和折半插入排序两种, ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 147. Insertion Sort List
Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
随机推荐
- Geography's sum up
1.世界气候: 热带草原气候,热带雨林气候,热带沙漠气候,热带草原气候 温带季风气候,温带大陆性气候,亚热带季风和湿润性气候,温带海洋性气候 寒带气候,高原山地气候. 2.亚洲气候: 1.大陆性气候分 ...
- LiteOS-任务篇
目录 前言 链接 参考 笔录草稿 基本概念 任务相关概念 LiteOS 任务运作机制 内核初始化 创建任务 创建任务有两种方案 任务相关函数 任务开发流程 创建创建任务 部分源码 例子 创建任务的任务 ...
- MQTT消息队列压力测试
环境准备: jmeter插件下载:mqttxmeter1.0.1jarwithdependencies.jar 把MQTT插件放在 %JMeter_Home%/lib/ext下.重启jmeter. M ...
- 阿里云服务器安装mongodb并且启动
// 1.下载 我是直接在local里面创一个mongodb文件夹进行下载和解压 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_ ...
- 记录小坑-tp5 使用模型select查询
场景: 使用模型去select查询后进行业务处理 再进行 saveAll 提示缺少更新条件 坑点:此时取出的数据结构是 query对象 { array:[ xxxx => xxx ] }: sa ...
- 解决:npm install ERR! Unexpected end of JSON input
npm ERR! Unexpected end of JSON input npm i -g npm@5 npm install --registry=https://registry.npm.tao ...
- 多测师讲解pthon_re模块_高级讲师肖sir
#import re 一.我们就re模块(也叫正则模块)介绍: 实现一个编译查找,一般在日志处理或者文件处理时用的比较多 正则表达式主要用于模式匹配和替换工作. 预定义字符集匹配: \d: ...
- [leetcode] 周赛 211
比赛题目:https://leetcode-cn.com/circle/discuss/luvHfG/ 两个相同字符之间的最长子字符串 题目:5543. 两个相同字符之间的最长子字符串. 开始理解错题 ...
- js 无刷新文件上传 (兼容IE9 )
之前项目中有个文件上传了需求,于是直接就使用了FormData对象异步上传,但是在测试得时候发现ie9无法正常上传(项目要求兼容IE9+),无奈,查资料得知IE9- 版本不支持formdata对象得异 ...
- docker的常用操作之二:docker内无法解析dns之firewalld设置等
一,如何启动一个已退出的容器? [root@localhost ~]# docker start storage4 说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.c ...