147.Insertion Sort List---链表排序(直接插入)
题目大意:对链表进行插入排序。
解法:直接插入排序。代码如下(耗时40ms):
public ListNode insertionSortList(ListNode head) {
ListNode first = new ListNode(0);
ListNode pre = first, cur = head, post = null;
while(cur != null) {
//保存cur.next,因为要遍历当前结点,下一次就要遍历当前结点的下一个结点,所以在这次遍历完之后需要重新赋值cur=post
post = cur.next;
//寻找可以插入的结点位置
while(pre.next != null && pre.next.val < cur.val) {
pre = pre.next;
}
//找到之后,将cur结点插入在pre和pre.next之间
cur.next = pre.next;
pre.next = cur;
//下一次pre再从头开始找可插入的结点位置,所以要置为开始头节点
pre = first;
//下一次对cur.next结点进行排序,所以要将cur置回
cur = post;
}
return first.next;
}
147.Insertion Sort List---链表排序(直接插入)的更多相关文章
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 147 Insertion Sort List 链表插入排序
用插入排序对链表进行排序. 详见:https://leetcode.com/problems/insertion-sort-list/description/ Java实现: 链表的插入排序实现原理很 ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] Sort List 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序, ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- [leetcode sort]147. Insertion Sort List
Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
- Insertion Sort List——链表的插入排序
Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...
随机推荐
- hdu3507 Print Article(斜率优化入门)(pascal)
Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique, he s ...
- [BZOJ4027]兔子与樱花
4027: [HEOI2015]兔子与樱花 Time Limit: 10 Sec Memory Limit: 256 MB Description 很久很久之前,森林里住着一群兔子.有一天,兔子们突 ...
- CF1093F Vasya and Array DP
题面 题面 \(\Delta\)题面有点问题,应该是数列中没有长度大于等于\(len\)的连续数字才是合法的. 题解 设\(f[i][j]\)表示DP到\(i\)位,以\(j\)为结尾的方案数, \( ...
- 【BZOJ1226】学校食堂(动态规划,状态压缩)
[BZOJ1226]学校食堂(动态规划,状态压缩) 题面 BZOJ 洛谷 题解 发现\(b\)很小,意味着当前这个人最坏情况下也只有后面的一小部分人在他前面拿到饭. 所以整个结果的大致顺序是不会变化的 ...
- 【BZOJ2436】【NOI2011】NOI嘉年华(动态规划)
[BZOJ2436]NOI嘉年华(动态规划) 题面 BZOJ 题解 考虑第一问如何求解 发现状态与选择了哪些活动无关,只与时间有关 设\(f[i][j]\)表示前\(i\)个单位时间(离散后),一个嘉 ...
- [学习笔记]Min-25筛
%%yyb %%zsy 一. 基本操作:筛1~N中的素数个数.n=1e9 设F(M,j)表示,2~M的所有数中,满足以下条件之一的数的个数:①x是质数②x最小质因子大于(注意是大于没有等号)$P_j$ ...
- Codeforces Round #306 (Div. 2)A B C D 暴力 位/暴力 暴力 构造
A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- STL源码分析-deque
http://note.youdao.com/noteshare?id=66f21dca07c1984f41848700021644fd
- ubuntu 16.04 镜像下载
下载地址: http://mirror.pnl.gov/releases/xenial/ Ubuntu 14.04.5 LTS (Trusty Tahr)http://releases.ubuntu. ...
- mysql 自动记录数据最后修改时间
原文 -- mysql ,还真有这样的说法: mysql> create table test( ), -> uptime timestamp on update current_time ...