【leetcode】147. Insertion Sort List
Sort a linked list using insertion sort.
链表的插入排序。
需要创建一个虚拟节点。注意点就是不要节点之间断了。
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* newhead = new ListNode(-);
newhead->next=head;
ListNode* q=head;
ListNode* p=head->next;
while(p!=NULL){
ListNode* s1=newhead;
ListNode* s2=s1->next;
while(s2!=p&&s2->val<=p->val){
s1=s2;
s2=s2->next;
}
if(s2!=p){
s1->next=p;
q->next=p->next;
p->next=s2;
p=q->next;}
else{
q=p;
p=p->next;
}
}
return newhead->next;
}
};
【leetcode】147. Insertion Sort List的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【刷题-LeetCode】147 Insertion Sort List
Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- 【leetcode】1122. Relative Sort Array
题目如下: Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 ar ...
- 【LeetCode】280. Wiggle Sort 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...
- 【LeetCode】791. Custom Sort String 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...
- 【leetcode】280.Wiggle Sort
原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- 极简单的方式序列化sqlalchemy结果集为JSON
继承 json.JSONEncoder 实现一个针对sqlalchemy返回类型的处理方式. sqlalchemy的返回类型有大都有两种,一种是Model对象,一种是Query集合(只查询部分字段). ...
- CJOJ 2022 【一本通】简单的背包问题(搜索)
CJOJ 2022 [一本通]简单的背包问题(搜索) Description 设有一个背包可以放入的物品重量为S,现有n件物品,重量分别是w1,w2,w3,-wn. 问能否从这n件物品中选择若干件放入 ...
- Linux基础(八)
一.shell shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个就是shell脚本.Python也是一种解释语言. 1. Linux中命令是按照下面的优先级执行的 ==& ...
- C语言基础 - 实现动态数组并增加内存管理
用C语言实现一个动态数组,并对外暴露出对数组的增.删.改.查函数 (可以存储任意类型的元素并实现内存管理) 这里我的编译器就是xcode 分析: 模拟存放 一个 People类 有2个属性 字符串类型 ...
- .babelrc 文件
文件干啥用的 babel是降es6转义成浏览器能理解的es5语法. 如果项目中用了babel 转移,需要定义babel需要的插件和预设转码. babel 一般可以配合 webpack . browse ...
- 【ECHART】实例
1. 带timeline datazoom的例子 <!doctype html> <html> <head> <meta http-equiv=" ...
- 微信客户端+微信公众平台+新浪云SAE+Arduino+WS100(控制LED)
第一步:准备 1.智能手机微信客户端或微信电脑版 2.注册微信公众平台 https://mp.weixin.qq.com 3.注册新浪账号 http://www.sinacloud.com 4.拥有一 ...
- 表达式求值(二叉树方法/C++语言描述)(一)
使用二叉树对算数表达式(以下简称为表达式)进行求值,实质上是将表达式转换为二叉树,对其进行后序遍历,得到后缀表达式的同时可以求得表达式的值.转换和求值的过程也需要借助数据结构栈的帮助. 二叉树数据结构 ...
- struts2增删改查---layer---iframe层
在这里写一下struts2中的简单的增删改查 struts.xml中的配置 <?xml version="1.0" encoding="UTF-8" ?& ...
- MJRefresh 使用注意点
1.使用上拉加载更多, 当数据加载完调用[ableView.mj_footer endRefreshingWithNoMoreData]方法显示已经全部加载完毕这个方法,一定要放在[tableView ...