【leetcode】Insertion Sort List (middle)
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)的更多相关文章
- 【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 ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【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. 思路:不能用 ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【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 ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 安装cocopods
http://www.tuicool.com/articles/7VvuAr3 OS 最新版 CocoaPods 的安装流程 1.移除现有Ruby默认源 $gem sources --remove h ...
- LYDSY模拟赛day9 2048
/* 大模拟题,做的时候思路还是比较清晰的 */ #include<iostream> #include<cstdio> #include<string> #inc ...
- JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件
现在很多的网站都提供有用户注册功能, 通常我们注册成功之后就会收到一封来自注册网站的邮件.邮件里面的内容可能包含了我们的注册的用户名和密码以及一个激活账户的超链接等信息.今天我们也来实现一个这样的功能 ...
- linux下svn命令使用大全
最近经常使用svn进行代码管理,这些命令老是记不住,得经常上网查,终于找了一个linux下svn命令使用大全:1.将文件checkout到本地目录 svn checkout path(path是服务器 ...
- Mac下的串口通信-ORSSerialPort
================================2015/11/05======================================= 最近在工作中遇到有关Mac下串口通信 ...
- java中的方法重载与重写以及方法修饰符
1. 方法重载Overloading , 是在一个类中,有多个方法,这些方法的名字相同,但是具有不同的参数列表,和返回值 重载的时候,方法名要一样,但是参数类型和参数个数不一样,返回值类型可以相同,也 ...
- bootstrap 响应式布局
先上点媒体查询css(某个著名的”段子“),跟bootstrap无关: <html> <head> <style> body { background-color: ...
- [ruby on rails] 深入(2) ruby基本语法
1. 调试&注释&打印输出 1.1 调试 ruby属于解释型语言,即脚本,在linux上,脚本的执行无非三种: 1. 用解释器运行脚本 解释器 脚本文件 即:ruby 脚本文件 2. ...
- ZJOIDay2T1 BB题解
讲道理我是调不出来了... 考虑对序列按下标维护每个节点最后的树. 那么 改操作点 - 把一段连续的节点改父亲 加点/删点(注意拆成两个操作了) 插儿子 那么用seg维护一下下标, 用ETT维护Dep ...
- phpcms--模型管理,推荐位管理,类别管理
phpcms的默认设置不一定能满足需求,这个时候必须启用[模型管理],[推荐位管理],[类别管理]三个高级功能 为什么需要使用这些功能呢,因为后台添加内容的时候需要不同的模型 而模型通过什么来展现呢, ...