问题描写叙述

对一个单链表进行插入排序,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. 对Man-In-The-Middle攻击的一点理解

    1)    中间人攻击最容易理解的可能就是fiddler吧,他可以截获request重新组织request的数据,有个专业的攻击工具叫burp. 2)    数据存在immutable,mutable ...

  2. springcloud11----turbine

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  3. extern--C#调用C++等其他非托管代码

    例如: https://www.cnblogs.com/sosoft/p/extern.html

  4. bzoj2721 / P1445 [Violet]樱花

    P1445 [Violet]樱花 显然$x,y>n$ 那么我们可以设$a=n!,y=a+t(t>0)$ 再对原式通分一下$a(a+t)+ax=x(a+t)$ $a^{2}+at+ax=ax ...

  5. 2018-2019-1 20189215 《Linux内核原理与分析》第四周作业

    <庖丁解牛>第三章书本知识总结 计算机的三大法宝 存储程序计算机 函数调用堆栈 中断 操作系统的两把宝剑 中断上下文的切换--保存现场和恢复现场 进程上下文的切换 Linux内核源码的目录 ...

  6. HBuilder 获取通话记录 (Android)

    代码: Date.prototype.Format = function (fmt) { var o = { , //月份 "d+": this.getDate(), //日 == ...

  7. 【eclipse】svn在线安装

    Subclipse1.8 http://subclipse.tigris.org/update_1.8.x

  8. centos7 Java开发环境构建

    原帖 https://www.cnblogs.com/youcong/p/9118753.html Java开发基本环境 1.jdk的安装 https://www.cnblogs.com/zenghu ...

  9. php中正则表达式的语法规则

  10. PHP通过日志来发现问题

    三大日志 1.Nginx的错误日志, Nginx的Access日志 2.PHPfpm的错误日志可设置级别 3.Mysql的慢查询日志