[LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort.
问题:实现单向链表的插入排序。
这是比较常规的一个算法题目。 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的对应位置中。
需要注意的一定是,列表只能定位下一个元素,不能定位前一个元素,所有,每次插入位置的适合,都是对左右指针的下一个元素进行操作。
void insertSort(ListNode* p1, ListNode* p2){
ListNode* next2 = p2->next;
p2->next = p2->next->next;
next2->next = p1->next;
p1->next = next2;
}
ListNode* insertionSortList(ListNode* head) {
if (head == NULL){
return head;
}
ListNode* r = head;
while(r->next != NULL){
ListNode* l = new ListNode();
l->next = head;
while(l != r){
if (l->next->val > r->next->val){
if (l->next == head) {
head = r->next;
}
insertSort(l, r);
break;
}
l = l->next;
}
if (r->next != NULL && r->next->val >= r->val) {
r = r->next;
}
}
return head;
}
[LeetCode] 147. Insertion Sort List 解题思路的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: 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 ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- [leetcode] 147. Insertion Sort List (Medium)
原题 别人的思路 非常简洁 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} h ...
- Leetcode#147 Insertion Sort List
原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- [leetcode sort]147. Insertion Sort List
Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
随机推荐
- 二分-hdu-4768-Flyer
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目意思: 有n个A.B.C,每个Ai,Bi,Ci,对于每个P=Ai+k*Ci(P<=B ...
- Oracle按用户进行统计信息更新
按用户进行统计信息更新 PL/sqldev工具使用system用户连接到oracle,打开命令窗口执行以下SQL,用户名请根据实际情况进行更改: begin dbms_stats.gather_sch ...
- JavaScript中几个可以转化为false的值
1.[0,NaN,“”,null,undefined]都可以直接转化为false,但这几个值不是完全相等的 var arr = [0,"",false,null,undefined ...
- Java-20个非常有用的程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1.字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string ...
- C++线性序列容器<vector>简单总结
C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...
- android studio adb 打不开
1.cmd-->C:\Users\Administrator>adb start-serveradb server is out of date. killing...error: cou ...
- 在iOS8下使用CLLocationManager定位服务需要系统授权
最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下 ...
- 【转】深入理解Java内存模型(七)——总结
处理器内存模型 顺序一致性内存模型是一个理论参考模型,JMM和处理器内存模型在设计时通常会把顺序一致性内存模型作为参照.JMM和处理器内存模型在设计时会对顺序一致性模型做一些放松,因为如果完全按照顺序 ...
- 【转】深入理解Java内存模型(四)——volatile
volatile的特性 当我们声明共享变量为volatile后,对这个变量的读/写将会很特别.理解volatile特性的一个好方法是:把对volatile变量的单个读/写,看成是使用同一个监视器锁对这 ...
- 重新认识Swift中的可选型(Swift2.1)
//: Playground - noun: a place where people can play import UIKit /* Swift中nil代表是是另外一种类型, 而不像OC那样, 任 ...