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. SQLServer安装错误之--->无法打开项 UNKNOWN\Components\DA42BC89BF25F5BD0AF18C3B9B1A1EE8\c1c4f01781cc94c4c8fb1542c0981a2a

    – 错误 1402.无法打开项 UNKNOWN\Components\7ABFE44842C12B390AF18C3B9B1A1EE8\54D21D49F3A8C1C49AC11A1B6445A83E ...

  2. ngCordova插件安装使用

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

  3. 计划安装SQL Server2012需求详细

    1.查看 SQL Server2012 安装的安装要求.系统配置检查和安全注意事项. 1.1 硬件要求 [参考资料http://msdn.microsoft.com/zh-cn/library/ms1 ...

  4. VBA 表操作1

    VBA 新建表.批量建表 例1 创建一个工作簿 注意 .name 与 .range ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  5. nyoj 91 阶乘之和(贪心)

    阶乘之和 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数)的阶乘之和,如9=1!+2!+3! ...

  6. 新年新技术:HTTP/2

    新的一年,项目也要带着发展的眼光往前走,得跟上潮流,当然前提是自己真的用的上. 用的上用不上都得先简单了解下. 2月下旬Google发布了首个基于HTTP/2的RPC框架GRPC,它是基于HTTP/2 ...

  7. XDU 1161 - 科协的数字游戏II

    Problem 1161 - 科协的数字游戏II Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 112  ...

  8. html 补充

    替换文本属性(Alt)alt 属性用来为图像定义一串预备的可替换的文本.替换文本属性的值是用户定义的.<img src="boat.gif" alt="Big Bo ...

  9. CHAP算法C++实现

    CHAP是一种挑战响应式协议. CHAP全称为:Challenge Handshake Authentication Protocol. CHAP密码 = 一个字节的标识符 + MD5(一个字节的标识 ...

  10. django xadmin 外键

    style_fields = {'db栏位名称': "fk-ajax"} 实体关系: Account (*)-->(1) user 表单控件: 下拉框 美化用了selecti ...