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的更多相关文章

  1. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. 【刷题-LeetCode】147 Insertion Sort List

    Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...

  3. LeetCode OJ 147. Insertion Sort List

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

  4. 【leetcode】1122. Relative Sort Array

    题目如下: Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 ar ...

  5. 【LeetCode】280. Wiggle Sort 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...

  6. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  7. 【leetcode】280.Wiggle Sort

    原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...

  8. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Python3 常用数据类型语法

    1.int类型 int类型的数据是没有长度限制的,它的最大长度只与计算机的内存有关. bin(i)      返回二进制表示结果, hex(i)      十六进制, int(i)       整数( ...

  2. Java读取数据源相关信息

    一.采用读取数据源配置文件的方式 package com.ofsp.utils; import java.io.IOException; import java.io.InputStream; imp ...

  3. Linux配置LNMP环境(一)配置Nginx

    注意:配置版本nginx-1.12.0,使用虚拟机安装linux.教程中的下载地址可能有变化.注意(在您看本教程之前首先需要对linux的基本操作熟悉) 1.  先cd到/usr/local/src ...

  4. FileMethods

    }else { System.out.println("不能写入此文件"); } System.out.println("此文件最后修改时间是2000年1月1日后的&qu ...

  5. JavaWeb 后端 <六> 之 EL & JSTL 学习笔记

    一.EL表达式(特别重要)

  6. 是否使用安全模式启动word

          打开word,出现了一个提示,显示着“word遇到问题需要关闭.我们对此引起的不便表示抱歉.”下面有选项“恢复我的工作并重启word”,选中它.点下面的“不发送”.      在出现的提示 ...

  7. PhysicsBasedAnimation学习记录

    一.前言 1.概述 Google I/O'17推出了许多新的特性,在动画这一块又有新的API供开发者使用,在动画API中引入了DynamicAnimation,开发者可以使用新的API创建更加动态化的 ...

  8. 使用Lucene.net+盘古分词实现搜索查询

    这里我的的Demo的逻辑是这样的:首先我基本的数据是储存在Sql数据库中,然后我把我的必需的数据推送到MongoDB中,这样再去利用Lucene.net+盘古创建索引:其中为什么要这样把数据推送到Mo ...

  9. 初涉算法——C++

    一.sstream头文件运用 题目:输入数据的每行包括若干个(至少一个)以空格隔开的整数,输出每行中所有整数之和. #include<iostream> #include<cstri ...

  10. css3变换,过度,动画实现梦幻网页

    html和css3一出,整个互联网设计发生了颠覆性的改变,各大IT企业也推出了很多新颖的设计,比如百度浏览器的下载首页,fullpage设计风格加css动画让网页看起来很流畅舒服. css3的变换有3 ...