问题描写叙述

对一个单链表进行插入排序,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. BabelMap 10.0.0.3 汉化版已经发布

    新的 BabelMap 在日前发布. 新版本增加了字符书签的管理功能,以及将窗口最小化到系统通知栏(时钟区域)的功能. 请点击主页左上角进入下载页面下载.

  2. c++第二十五天

    p129~p131: 1.赋值运算的左侧运算对象必须是一个可修改的左值. 2.赋值运算满足右结合律. 3.赋值运算的结果是它的左侧对象,并且是一个左值. 验证: #include<iostrea ...

  3. Stitching模块中leaveBiggestComponent初步研究

    在Stitching模块中以及原始论文<Automatic Panoramic Image Stitching using Invariant Features>3.2中,都有" ...

  4. [微信开发] - UnionID以及微信开放平台

  5. 组学航母----OMICtools

    OMICtools可谓是组学研究的航空母舰,其收集了基因组学.转录组学.蛋白质组学和代谢组学等分析研究常用的4400余个工具和数据库.它允许用户submit自己的工具/数据库,每一个上传的工具/数据库 ...

  6. 详解Python中re.sub--转载

    [背景] Python中的正则表达式方面的功能,很强大. 其中就包括re.sub,实现正则的替换. 功能很强大,所以导致用法稍微有点复杂. 所以当遇到稍微复杂的用法时候,就容易犯错. 所以此处,总结一 ...

  7. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. 使用 shinydashboard

    除了 shiny 扩展包提供的函数之外,RStudio 也开发了一个 shinydashboard 扩展包 (http://rstudio.github.io/shinydashboard/),它呈现 ...

  9. java中interrupt、join、sleep、notify、notifyAll、wait详解

    首先介绍一下中断概念:举个例子容易理解一点 例子:假如你正在给朋友写信,电话铃响了.这时,你放下手中的笔,去接电话.通话完毕,再继续写信.这个例子就表现了中断及其处理过程:电话铃声使你暂时中止当前的工 ...

  10. vSphere Client的拷贝 粘帖 功能

    Windows Client OS的情况下, Remote Desktop 自带拷贝/粘帖 功能, 所以一直没在意. 这回用CentOS, 比起vnc viewer , 感觉还是自带的 vSphere ...