题目

Sort a linked list using insertion sort.

分析

实现链表的插入排序

注意:

  1. 程序入口的特殊输入判断处理!
  2. 节点的链接处理,避免出现断链!

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;
}
};

GitHub测试程序源码

LeetCode(147) Insertion Sort List的更多相关文章

  1. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  2. 新概念英语(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 ...

  3. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. 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 ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. (转)Centos7安装配置NFS服务和挂载

    Centos7安装配置NFS服务和挂载 原文:https://www.u22e.com/601.html NFS简介 NFS(Network File System)即网络文件系统,是FreeBSD支 ...

  2. 下一代的前端构建工具:parcel打包react

    1. parcel很受欢迎,webpack太慢了,试试Parcel下一代的前端构建工具 2.Parcel很快,但缺少好多插件,没有base64,没有办法拆分打包文件.... 3.总结:适合小项目 4. ...

  3. JDBC和分包

    JDBC(Java Data Base Connectivity,java数据库连接) JDBC是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...

  4. Android仿360悬浮小球自定义view实现

    转载请标明出处:http://www.jianshu.com/u/a5ad093cffe8 效果图如下: 图片.png   图片.png 实现当前这种类似的效果 (360小球 悬浮桌面差不错类似).第 ...

  5. [Java]获取byte数组的实际使用长度

    背景:byte.length只能获取到初始化的byte数组长度,而不是实际使用的长度,因此想要获取到实际的使用长度只能靠其他方法实现. 方法一: public class ByteActualLeng ...

  6. 内容导出成word

    private void 导出word(string 内容) { string tit = "<html xmlns:v=\"urn:schemas-microsoft-co ...

  7. Outlook 客户端无法通过 MAPI over HTTP协议 连接

    随着Exchange 版本更新升级,是否进行验证客户端建立MapiHttp连接所需的服务器设置已正确配置.即使服务器,负载均衡器和反向代理的所有设置都正确,您可能会遇到连接到Exchange Serv ...

  8. 动态生成带参数的html标签

     "<button onclick='watchClick("+'"'+row.BOXNO + '","'+ row.VOY_NO+'" ...

  9. TC和脚本语言

    TC:Turbo C 集成开发环境是由Borland 公司开发的一套C 语言开发工具,它集成了程序编辑.调试.链接等多种功能.在DOS 系统时代,Turbo C 是被最广泛使用的一种PC 机应用程序开 ...

  10. 并发教程--JAVA5中 计数信号量(Counting Semaphore)例子

    并发教程--JAVA5中 计数信号量(COUNTING SEMAPHORE)例子 本文由 TonySpark 翻译自 Javarevisited.转载请参见文章末尾的要求. Java中的计数信息量(C ...