Sort a linked list using insertion sort.

思路:

用插入排序对链表排序。插入排序是指每次在一个排好序的链表中插入一个新的值。

注意:把排好序的部分和未排序的部分完全分开,指针不要有交叉。 即不会通过->next 重叠

class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL)
return NULL; ListNode * ans = head;
head = head->next;
ans->next = NULL; //排好序的第一个结点,后面跟的要是NULL 排好序的部分和未排好序的部分不能重合
while(head != NULL) //还有要插入的 每次都把剩下还未排序部分的头结点插入
{
ListNode * pans = ans; //记录插入的位置
ListNode * tmp = NULL; //交换时用的临时的值
if(pans->val >= head->val) //新来的结点最小,是新的头结点
{
tmp = head;
head = head->next;
tmp->next = pans;
ans = tmp;
continue;
}
//找到要插入的前一个指针
while(!(pans->val < head->val && (pans->next == NULL || pans->next->val >= head->val))) //比当前结点大,且小于等于后面的结点值,或后面的结点值为0
{
pans = pans->next;
}
tmp = head;
head = head->next;
tmp->next = pans->next;
pans->next = tmp;
}
return ans;
}
};

【leetcode】Insertion Sort List (middle)的更多相关文章

  1. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  6. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  7. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  9. 【leetcode】Divide Two Integers (middle)☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

随机推荐

  1. ngCordova插件安装使用

    为什么ngCordova ngCordova是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在Angul ...

  2. tyvj1614 魔塔魔塔!

    描述 百度noip贴吧管理组开发了一个小游戏,叫魔塔魔塔.虽然把魔塔重复了两次,但其实还只是个魔塔而已,还是简化版的.游戏在一个N*M大小的地图中进行,每一格都是正方形.对于某一格,有若干种可能的状态 ...

  3. ajxa

    ajxa上传文件提交: ajxa跨域:http://www.cnblogs.com/sunxucool/p/3433992.html http://www.cnblogs.com/fsjohnhuan ...

  4. linux下的库冲突问题

    lib1.c #include <stdio.h>int fun(){ printf("lib1\n"); return 0;} lib2.c #include < ...

  5. oracle 使用ID关键字作列名导致索引失效

    oracle表空间变更导致主键索引失效,重建索引即可

  6. eclipse无法自动识别出svn项目

    因为重新安装了svn插件,重启后发现原来的svn项目无法自动识别出来,连Team->Share Project都没有,而本地用tortoiseSvn是可以正常操作的. 后来我把项目删除然后重新导 ...

  7. python入门基础代码

    #查找index函数的帮助 help(str.index) #for循环和break语句from math import sqrtfor i in range(2,101): flag=1 k=int ...

  8. linux 下开放端口问题

    Linux安装Tomcat后本地可以正常访问,可是这时Tomcat还不能被外界访问需要在Linux默认防护墙上打开8080端口 打开 /etc/sysconfig/iptables   [root@l ...

  9. JAVA序列化的作用

    所谓的Serializable,就是java提供的通用数据保存和读取的接口.至于从什么地方读出来和保存到哪里去都被隐藏在函数参数的背后了.这样子,任何类型只要实现了Serializable接口,就可以 ...

  10. BZOJ4411——[Usaco2016 Feb]Load balancing

    1.题意: 给出N个平面上的点.保证每一个点的坐标都是正奇数. 你要在平面上画两条线,一条是x=a,一条是y=b,且a和b都是偶数. 直线将平面划成4个部分,要求包含点数最多的那个部分点数最少. 2. ...