问题描写叙述

对一个单链表进行插入排序,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. kylin与superset整合

    前提: kylin安装以及配置可以参考 https://www.cnblogs.com/654wangzai321/p/9676204.html 我这边用的Linux自带的python2.7,为了保证 ...

  2. 使用Linux重定向解决nohup.out无写权限问题

    ■场景 执行nohup命令的时候,经常会出现下面这种没有写入权限的错误. nohup: ignoring input and appending output to `nohup.out'nohup: ...

  3. 2018-2019-1 20189218《Linux内核原理与分析》第二周作业

    问题一 动态库链接找不到库问题 这个问题当时确实对我造成了很大的困扰,虽然最终仍然成功用动态库链接但是问题并没有解决.现在回过头来看却觉得有点蠢,但出错的过程仍然值得总结.首先看我的目录结构: 可以看 ...

  4. 20145319 return-to-libc攻击实验

    20145319 Return-to-libc攻击实验 一 实验内容 return-to-libc实验是一个基于缓冲区溢出攻击实验的基础上的一种攻击实验 缓冲区溢出攻击相关知识: 原理:通过一段包含s ...

  5. 再也不学AJAX了!(三)跨域获取资源 ③ - WebSocket & postMessage

    让我们先简单回顾一下之前谈到的内容,AJAX是一种无页面刷新的获取服务器资源的混合技术.而基于浏览器的"同源策略",不同"域"之间不可以发送AJAX请求.但是在 ...

  6. UOJ #266 【清华集训2016】 Alice和Bob又在玩游戏

    题目链接:Alice和Bob又在玩游戏 这道题就是一个很显然的公平游戏. 首先\(O(n^2)\)的算法非常好写.暴力枚举每个后继计算\(mex\)即可.注意计算后继的时候可以直接从父亲转移过来,没必 ...

  7. python 判断列表的包含关系

    def is_Sublist(l, s): sub_set = False if s == []: sub_set = True elif s == l: sub_set = True elif le ...

  8. json文件为空时读取会报错

    simplejson.errors.JSONDecodeError: Expecting value: line column () 提示说是解码错误 可以用下面的方法判断json文件是否为空 imp ...

  9. USB.资料

    1.百度搜索 “usb java” 1.1.基于usb4java实现的java下的usb通信 - tomi_mint - 博客园.html(https://www.cnblogs.com/sowhat ...

  10. 235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

    235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes ...