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) ...
 
随机推荐
- android面试(2)----组件
			
1.anroid:id的作用? android:id是作为控件的唯一标示符.可以使用与releativelayout中,也可以再Activity中通过findviewbyid来获得指定的控件. 2.a ...
 - html/css/js 学习笔记  - 牛客网试卷:前端工程师能力评估
			
display属性 : block : CSS1 块对象的默认值.将对象强制作为块对象呈递,为对象之后添加新行 可以定义高度和宽度 none : CSS1 隐藏对象.与 visibility 属性 ...
 - 【BZOJ4443】小凸玩矩阵(二分答案,二分图匹配)
			
[BZOJ4443]小凸玩矩阵(二分答案,二分图匹配) 题面 BZOJ Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两 ...
 - Unity3D手游开发日记(3) - 场景加载进度条的完美方案
			
我以为做个进度条很简单,分分钟解决,结果折腾了一天才搞定,Unity有很多坑,要做完美需要逐一解决. 问题1:最简单的方法不能实现100%的进度 用最简单的方法来实现,不能实现100%的进度,原因是U ...
 - 【状压DP】【CF8C】 Looking for Order
			
传送门 Description 给你n个点,每次可以从起点到最多两个点然后回到起点.求经过每个点最少一次的最短欧氏距离和是多少 Input 第一行是起点的坐标 第二行是点的个数\(n\) 下面\(n\ ...
 - SFM学习
			
摘自李翠http://www.cnblogs.com/serser/p/6598621.html SFM 1.相机模型,内参数和外参数矩阵,相机标定: 2.极线约束和本征矩阵:特征点提取与匹配:提取到 ...
 - Codeforces 932.D Tree
			
D. Tree time limit per test 2 seconds memory limit per test 512 megabytes input standard input outpu ...
 - poj2549 Sumsets
			
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11886 Accepted: 3273 Descript ...
 - Codeforces 717.F Heroes of Making Magic III
			
F. Heroes of Making Magic III time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
 - php的自动加载函数spl_autoload_register和__autoload
			
spl_autoload_register和__autoload是用来自动加载类的,不用每次都require,include这样搞. 先说__autoload的用法, 在同级目录建立2个文件,一个in ...