问题描写叙述

对一个单链表进行插入排序,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. uva11020 set

    有n个人,每个人有两个属性x,y.如果对于一个人P(x,y) 不存在另外一个人(x',y') 使得x'<x,y'<=y 或者 x'<=x,y'<y 我们说p是有优势的,每次给出 ...

  2. bzoj1623 / P2909 [USACO08OPEN]牛的车Cow Cars

    P2909 [USACO08OPEN]牛的车Cow Cars 显然的贪心. 按速度从小到大排序.然后找车最少的车道,查询是否能填充进去. #include<iostream> #inclu ...

  3. constructor-arg和property的区别

    两者都是给bean注入属性,区别: constructor-arg:通过构造函数注入. property:通过setter对应的方法注入. 详情见:https://blog.csdn.net/u012 ...

  4. 辅助模块应用(auxiliary/scanner/portscan/tcp)

    实验步骤 创建msf所需的数据库 之前我们开启msf时下面总会出现一个红色的小减号,原来是因为没有和数据库键连接,于是首先我们要手动建立一个数据库... 使用命令来实现: service postgr ...

  5. 20162311 课堂测试 泛型类—Bag

    课堂测试 泛型类-Bag 目录 一.题目要求 二.设计思路 三.问题和解决办法 四.代码运行截图 五.代码托管地址 六.总结 一.题目要求 题目:泛型类-Bag 返回目录 二.设计思路 自定义一个Ba ...

  6. Tomcat 启动图解

    Tomcat server.xml结构 startup.bat执行流程 catalina.bat执行流程 Tomcat Server处理一个http请求的过程

  7. artTemplate 模板使用

    下载github中文件,浏览器引用lib/template-web.js 模板html: {{each ProductInfoList as prd}} <div class="res ...

  8. 51Nod 1596 搬货物

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1596 思路: 模拟二进制的进位. 这题很坑啊...用c++会超时,用c就 ...

  9. Android下拉刷新控件--PullToRefresh的简单使用

    Android中很多时候都会用到上下拉刷新,这是一个很常用的功能,Android的v4包中也为我们提供了一种原生的下拉刷新控件--SwipeRefreshLayout,可以用它实现一个简洁的刷新效果, ...

  10. ${user.home} is not working in jenkins windows system

    default setting create m2 in C:\Windows\system32\config\systemprofile change it to <localReposito ...