LeetCode(147) Insertion Sort List
题目
Sort a linked list using insertion sort.
分析
实现链表的插入排序
注意:
- 程序入口的特殊输入判断处理!
- 节点的链接处理,避免出现断链!
AC代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == NULL || !head->next)
return head;
ListNode *p = head->next;
head->next = NULL;
//从头结点的下一节点开始,遍历插入排序
while (p)
{
//保存p节点的后续节点
ListNode *r = p->next;
//判断是否应插入到头结点
if (p->val < head->val)
{
p->next = head;
head = p;
}
else{
//寻找p节点应插入位置的前驱
ListNode *pre = head;
while (pre->next && pre->next->val <= p->val)
{
pre = pre->next;
}
p->next = pre->next;
pre->next = p;
}
//循环下一节点的插入
p = r;
}//while
return head;
}
};
LeetCode(147) Insertion Sort List的更多相关文章
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- 新概念英语(1-47)A cup of coffee
新概念英语(1-47)A cup of coffee How does Ann like her coffee? A:Do you like coffee, Ann? B:Yes, I do. A:D ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- TDH-大数据基础
------------------------------------------------------------------------------------*******大数据概念和基础* ...
- TDH-search汇报理解
题目:海量数据查询开头:1.自我介绍:2.题目切入: 什么是海量数据查询?(海量数据,快速,符合要求) 几个常用场景(搜索引擎,百度:话单查询:影像平台,高铁)3.展示目录:架构,案例,平台规划 4. ...
- Unity注入
[此文引用别人,作为随笔自己看.]今天写<WCF技术剖析(卷2)>关于<WCF扩展>一章,举了“如何通过WCF扩展实现与IoC框架(以Unity为例)集成”(<通过自定义 ...
- go日志输入到es
1.依赖 github.com/alecthomas/log4go 2.配置 <filter enabled="true"><!-- enabled=false ...
- Flask 学习系列(三)---Jinjia2使用过滤器
再Jinjia2中过滤器是一种转变变量输出内容的技术.··过滤器通过管道符号“|与变量链接,并且可以通过圆括号传递参数” .举例说明: {{my_variable|default('my_variab ...
- 有关在python中使用Redis(二)
这里简单介绍下在python中使用如何使用hashset,set和list: 从list开始: 一般我们使用lpush对一个list进行初始化添加,但是如果需要不断往这个list里面加值,就要用rpu ...
- windows服务器安装安全狗时服务名如何填写
安全狗安装时“服务名”这一栏指的是apache进程的服务名称,即进入“任务管理-服务”里显示的名称. phpstudy等软件搭建的环境需要设置运行模式为“系统服务”后才能看到服务名.
- python 之网页解析器
一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- BZOJ 1806: [Ioi2007]Miners 矿工配餐
ime Limit: 10 Sec Memory Limit: 64 MBSubmit: 910 Solved: 559[Submit][Status][Discuss] Description ...