题目

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. 关于codeblock 为什么不能调试

    最近codeblock不能调试了,一开始还以为把断点放在了函数里面,所以不行. 代码短,就自己看了, 有时候实在不行,真的要调试,那怎么办?其实很多时候是你的文件名的问题. 1.project的路径必 ...

  2. Spark Mllib里的分布式矩阵(行矩阵、带有行索引的行矩阵、坐标矩阵和块矩阵概念、构成)(图文详解)

    不多说,直接上干货! Distributed  matrix : 分布式矩阵 一般能采用分布式矩阵,说明这数据存储下来,量还是有一定的.在Spark Mllib里,提供了四种分布式矩阵存储形式,均由支 ...

  3. css3背景与边框相关样式

    background-attachment          背景图像是否固定或者随着页面的其余部分滚动 background-color                    设置元素的背景颜色 b ...

  4. AngularJS(三):重复HTML元素、数据绑定

    本文也同步发表在我的公众号“我的天空” 重复HTML元素 在前端的页面编写中,我们会经常遇到重复HTML元素,譬如绘制表格.菜单等,如以下代码显示一个简单的li列表: <body>    ...

  5. Android笔记--Bitmap(二)内存管理

    Bitmap(二) 内存管理 1.使用内存缓存保证流畅性 这种使用方式在ListView等这种滚动条的展示方式中使用最为广泛, 使用内存缓存 内存缓存位图可以提供最快的展示.但代价就是占用一定的内存空 ...

  6. JavaScript_11_验证

    表单验证: JavaScript可用来在数据被送往服务器前对HTML表单中的输入数据进行验证 1. 是否填写了必填项目 2. 邮件地址是否合法 ... <form action="su ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. POJ Charlie's Change 查理之转换(多重背包,变形)

    题意: 给定身上的4种硬币,分别是1 ,5 ,10, 25面额各有多张,要求组成面额p的硬币尽可能多.输出组成p的4种硬币各自的数量. 思路: 多重背包,300+ms.用01背包+二进制的方法.记录下 ...

  9. Windows Experience Index

    The Windows Experience is still there--even in build 9860.  However, the GUI was retired with Window ...

  10. mysql+thinkphp +amcharts 完成图表统计功能

    思路:从mysql数据库查询数据,经过thinkphp 后端控制器做逻辑处理,返回给前端,前端调用Amcharts 插件 1.数据查询: public function order($time='', ...