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. 转:利用node压缩、合并js,css,图片

    1.安装nodejs http://nodejs.org/ 2.安装各自的node package js我用的是UglifyJS github地址:https://github.com/mishoo/ ...

  2. java之Date

    1.java时间处理 package com.bmkit.util.date; import java.text.DateFormat; import java.text.ParseException ...

  3. java中重载与重写的区别

    (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型. 重载Overloading是一个类中多态性的一种表现. 然后我们再来谈谈 重写(Over ...

  4. BZOJ2555——SubString

    0.题目很短,就不概括了 给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支持这些操作. ...

  5. sizeof和小部分c++语法

    ios 中 sizeof(类型) 获取类型所占字节 32位操作系统中, 每个字节占8个位 64位操作系统中, 每个字节占16个位 INT_MAX  2147483647 int size1 = siz ...

  6. 剑指Offer 调整数组顺序使奇数位于偶数前面

    题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变.     思路: ...

  7. Struts2 Action 动态传参数

    Struts2的两个Action之间传参的问题. 需求功能是这样:Action1 获取数据库配置内容,得到相应Model的 动态URL ,这里的URL 有的是Action有的是JSP页面. 1.使用r ...

  8. 一个简单的Python网络爬虫(抓图),针对某论坛.

    #coding:utf-8 import urllib2 import re import threading #图片下载 def loadImg(addr,x,y,artName): data = ...

  9. 线段树区间更新&&求和poj3486

    给出了一个序列,你需要处理如下两种询问. ≤ c ≤ ). "Q a b" 询问[a, b]区间中所有值的和. Input 第一行包含两个整数N, Q. ≤ N,Q ≤ . 第二行 ...

  10. Exception异常

    JAVA异常指的是运行期出现的错误. 观察错误的名字和行号最重要. 运用关键字try将可能出错的语句catch出来并添加友好的话 \ 在这句话中的ae代表一个自己命名的对象. 1.要捕获首先需要知道错 ...