题目

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. shell 经典

    使用新写法 这里的新写法不是指有多厉害,而是指我们可能更希望使用较新引入的一些语法,更多是偏向代码风格的,比如 尽量使用func(){}来定义函数,而不是func{} 尽量使用[[]]来代替[] 尽量 ...

  2. 前后端分离 vue+springboot 跨域 session+cookie失效问题

    环境: 前端 vue   ip地址:192.168.1.205 后端 springboot2.0  ip地址:192.168.1.217 主要开发后端. 问题: 首先登陆成功时将用户存在session ...

  3. linux安装源文件(.tar.gz)

    安装此类文件,分为7步: 1.首先把依赖的软件给装上,如果依赖perl,先装perl,如果依赖Pathon,现装pathon 2.tar 源软件路径 -C 新软件路径(注意这里一定要-C,不然不能解压 ...

  4. str中的join方法,fromkeys(),set集合,深浅拷贝(重点)

    一丶对之前的知识点进行补充 1.str中的join方法.把列表转换成字符串 # 将列表转换成字符串,每个元素之间用_拼接 s = "_".join(["天",& ...

  5. Kendo UI 单页面应用(四) Layout

    Kendo UI 单页面应用(四) Layout Layout 继承自 View,可以用来包含其它的 View 或是 Layout.下面例子使用 Layout 来显示一个 View <div i ...

  6. HBase数据模型(2)

    HBase数据模型(1) HBase数据模型(2) 1.0 HBase的版本version,是一个用长整型表示的.由Rowkey.Column(列族和列).Version组合在一起称为HBase中的一 ...

  7. 【js类库AngularJs】学习angularJs的指令(包括常见表单验证,隐藏等功能)

    [js类库AngularJs]学习angularJs的指令(包括常见表单验证,隐藏等功能) AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀 ...

  8. GIT SSH免登录密码实现更新(git pull)、推送(git push)操作

     一.使用场景 现在有两台服务器A和B,在A服务器上搭建有git版本代码仓库,现要实现B服务器SSH免密码登录A服务器,并能够从A服务器拉取.推送代码! 二.操作步骤 1.在B服务器项目根目录下执行以 ...

  9. 想系统化快速学习人工智能?上Azure School就够了!

    要说目前最热门的技术,非人工智能莫属了,让计算机程序能够看懂.听懂.读懂.理解我们的世界!想想就激动!! 上至高大上的个人数字化助理,下至P图软件,各种应用都开始增加AI相关的功能,试问又有哪个技术爱 ...

  10. Android(java)学习笔记111:Java异常分类

    Java异常可分为3种: (1)编译时异常:Java.lang.Exception (2)运行期异常:Java.lang.RuntimeException (3)错误:Java.lang.Error