147. Insertion Sort List (List)
Sort a linked list using insertion sort.
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(!head || !head->next) return head;
ListNode *tail =head->next;
head->next = NULL;
ListNode *current;
ListNode *tmp;
while(tail){
if(tail->val < head->val){
tmp = tail->next;
tail->next = head;
head = tail;
tail = tmp;
continue;
}
current = head;
while(current->next && tail->val >= current->next-> val) current = current->next;
tmp = tail->next;
tail->next = current->next;
current->next = tail;
tail = tmp;
}
return head;
}
};
147. Insertion Sort List (List)的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 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 ...
- 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 ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- 【leetcode】147. Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [leetcode sort]147. Insertion Sort List
Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
随机推荐
- ambassador kubernetes native api gateway
github 上的介绍: Ambassador is an open source Kubernetes-native API Gateway built on Envoy, designed for ...
- js中对数字进行正则判断
<script type="text/javascript"> function SubmitCk(num) { var reg = /^([a-zA-Z0-9]+[_ ...
- L2TP/IPSec一键安装脚本
本脚本适用环境:系统支持:CentOS6+,Debian7+,Ubuntu12+内存要求:≥128M更新日期:2017 年 05 月 28 日 关于本脚本:名词解释如下L2TP(Layer 2 Tun ...
- (新)解决php版本ueditor中动态配置图片URL前缀(imageurlprefix)的方法
昨天晚上写了一篇文章<解决ueditor中没法动态配置imageurlprefix的方法>,通过修改js获取当前域名的方法,配置imageurlprefix值: 发现还是不够灵活,因为域名 ...
- laravel里面使用event
模式:大概是通过一个自定义的event,一个handler,还有一个binder,然后用来简化通知模型 生成自定义的event ./artisan make:event MyEvent 生成自定义的h ...
- 1DAY 初识Python
一 本节目标 了解编程语言 了解python及与其他语言的优劣对比 安装python解释器及环境变量配置.运行python交互式环境 打印hello world程序 初识变量.用户输入,流程控制,wh ...
- java web 程序---猜数字游戏的猜了多少次的代码
思路:用setAttribute()放 ,然后直接输出 Integer str=(Integer)session.getAttribute("count"); int num3= ...
- 【洛谷】P3908 异或之和(异或)
题目描述 求1 \bigoplus 2 \bigoplus\cdots\bigoplus N1⨁2⨁⋯⨁N 的值. A \bigoplus BA⨁B 即AA , BB 按位异或. 输入输出格式 输入格 ...
- Android屏幕适配方案——基于最小宽度(Smallest-width)限定符
转自:https://www.cnblogs.com/error404/p/3815739.html 一.关于布局适配建议 1.不要使用绝对布局 2.尽量使用match_parent 而不是fill_ ...
- MonkeyScript测试命令集合
MonkeyScript:(不支持截屏) 可以被Monkey识别的集合命令 可以完成重复固定的操作 MonkeyRunner(支持截屏操作) 提供一系列API,可以完成模拟事件和截屏操作 Mo ...