问题描写叙述

对一个单链表进行插入排序,head指向第一个结点。

代码

/**
* 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||head->next==NULL)
return head;
ListNode *pstart=new ListNode(0);//加入一个头指针以方便处理。设全部节点数据大于0
pstart->next=head;
ListNode *preCur=head,*cur=head->next;
while(cur!=NULL)
{
ListNode *prePos=pstart,* pos=pstart->next;
while(pos->val<cur->val)
{
prePos=prePos->next;//prePos指向带插入位置的前方
pos=pos->next;//pos指向待插入位置的后方
}
if(pos!=cur)
{
preCur->next=cur->next;
cur->next=pos;
prePos->next=cur;
//preCur不变
cur=preCur->next;
}
else
{
preCur=cur;
cur=cur->next;
}
}
head=pstart->next;
delete pstart;
return head;
}
};

时间复杂度

O(N^2)。相对于顺序存储降低移动次数。

leetcode:Insert Sort List的更多相关文章

  1. Insert Sort Singly List

    对单链表插入排序,给出个单链表的head节点:返回排完序的head节点: 首先数据结构中习惯了以数组为参数排序,瞬间想到是遍历单链表存入arraylist中,再进行insert sort,(O(n** ...

  2. 计数排序(Count Sort )与插入排序(Insert Sort)

    计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...

  3. c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,

    #include <iostream.h> #define  MAX 100 void dispaly(int a[],int n) {     for(int i=0;i<n;i+ ...

  4. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  5. C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)

    leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/  Total Accepted: 68702 Total ...

  6. insert sort

    插入排序将数据分为前面有序部分和后面无序部分,取无序部分的第一个元素插入到有序序列中. 注意与选择排序的区别. // insert sortvoid insertionSort(int arr[], ...

  7. 待字闺中之快排单向链表;leetcode之Sort List

    题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...

  8. LeetCode——Insertion Sort List

    LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...

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

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

随机推荐

  1. 【运维技术】windows安装apache服务器,实现域名对应端口的解析跳转

    linux 安装参考搭建dede项目的功能 windows 安装虚拟机的指南参考:http://jingyan.baidu.com/article/29697b912f6539ab20de3cf8.h ...

  2. Aliexpress API 授权流程整理(转载)

    前言 我零零总总用了好几个月的时间,写了一个自用的小程序,从 Aliexpress 上抓取订单的小程序.刚开始写的时候,该API还没有开放,而且没有订单相关的功能.我完全是通过模拟用户在网页上的操作来 ...

  3. PHP中使用OpenSSL下openssl_verify验证签名案例

    使用OpenSSL那么需要先了解一下http://www.cnblogs.com/wt645631686/p/8390936.html <?php //demo $json = '{" ...

  4. 参考sectools,每个人至少查找5种安全工具、库等信息并深入研究至少两种并写出使用教程

    1.Nessus Nessus是免费网络漏洞扫描器,它可以运行于几乎所有的UNIX平台之上.它不仅能永久升级,还免费提供多达11000种插件(但需要注册并接受EULA-acceptance--终端用户 ...

  5. #include <ntifs.h>出现PEPROCESS redefinition问题处理

    转载:http://blog.csdn.net/ytfrdfiw/article/details/23334297 如果在自己的程序中,即包含ntddk.h和ntifs.h的时候,编译的时候会出现如下 ...

  6. sed删除包含指定字符串的所有行

    1.以删除文件example.txt中包含字符串"=yes"的行为例,example.txt文件有以下内容: dadasdfsadf=yes=sds dsdadasdkfk dsd ...

  7. 【乱码】运行java -jar xx.jar存到hbase里的数据乱码

    程序在Eclipse里运行没有问题,但是打成jar包之后写入hbase里的数据会有乱码,ES里正常 经过测试,运行命令里加上-Dfile.encoding=utf-8 就可以正常写入,但是cmd命令里 ...

  8. tomcat跟目录下work文件夹的作用

    work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录. jsp,tomcat的工作原理:当浏览器访问某个jsp页面时,tomcat会在work目录里把这个j ...

  9. hdu 1163 九余数定理

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  10. node 常用指令 node 扩展链接

    node -v       node 版本 npm -v     npm版本号,npm是在安装nodejs时一同安装的nodejs包管理器  (注册.安装模块,和小乌龟有点像) npm list  当 ...