【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 ...
随机推荐
- 如何确定C#代码是在编译时执行还是在运行时执行
突然想起那个"switch..case..."的case标签都可以判断哪些类型... 就先搞了一个错误的demo... class Program { static void Ma ...
- Ubuntu/Windows下利用“HIDAPI”库函数实现与Hid类USB设备通信
一.背景: 最近在做的一个项目需要使用USB传递数据,对USB理解不是很深,USB的系统驱动编写则更是天方 夜谭,因此将设备配置成HID类设备成为了首选.其最大的好处在于,LINUX/Windows系 ...
- tc 146 2 BridgeCrossing(n人过桥问题)
SRM 146 2 1000BridgeCrossing Problem Statement A well-known riddle goes like this: Four people are c ...
- C++ 模拟虚拟键盘按键表
键盘VK键值列表 /* Virtual Keys, Standard Set*/ VK_LBUTTON 0x01 VK_RBU ...
- Redis学习笔记九:独立功能之慢查询日志
Redis 的慢查询日志用于记录执行时间超过给定时长的命令请求,用户可以通过这个功能产生的日志来监视和优化查询速度. 服务器配置有两个相关选项: slowlog-log-slower-than 选项指 ...
- JQuery选择器细节-遁地龙卷风
1.层次选择器-子元素选择器 <body> <div> <p>lol</p> <div> <p></p> </ ...
- 用jQuery实现的一种网页内容呈现方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 移动端rem切图
1.为什么用rem 根据屏幕大小,自动调整大小 2.如何使用rem 分以下几步 a.用ps把设置稿弄成640px或者750px的(记得等比例缩放) b.调试时记得把浏览器默认最小字体设置为最小.手机端 ...
- cocos2d界面渲染
渲染是visit函数来做的, visit是先将不可见的节点和他所有的子节点都跳过, 然后再看节点的子节点是否为空, 如果为空的话直接看这个节点是否在摄像机可见范围之内, 如果在就渲染这个节点, 否则什 ...
- [BZOJ1503][NOI2004]郁闷的出纳员
[BZOJ1503][NOI2004]郁闷的出纳员 试题描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是 ...