[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. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
随机推荐
- Counting Lines, Words, and Characters with wc
Counting Lines, Words, and Characters with wc When working with text files, you sometimes get a ...
- rmi rpc restful soa 区别
rmi rpc restful soa 区别 rmi vs rpc 参考文档:http://stackoverflow.com/questions/2728495/what-is-the-differ ...
- Cors 跨域Access-Control-Allow-Origin
1.Access-Control-Allow-Origin 指定格式 The Origin header field has the following syntax: origin = " ...
- 打开的IE网页不是最大化的解决方法
方法一:先把所有的IE窗口关了;只打开一个IE窗口;最大化这个窗口;关了它;OK,以后的默认都是最大化的了 方法二:先关闭所有的IE浏览器窗口,用鼠标右键点击快速启动栏的IE浏览器图标,在出现的快捷菜 ...
- oracle 复制一条记录只改变主键不写全部列名
场景:表TEST中有C1,C2,C3...字段,其中C1为主键,先需要复制表TEST中一条(C1='1'的)记录,修改主键列C1和需要变更的列后,再插入到表TEST中. procedure P_TES ...
- 使用AFNetworking时, 控制器点击返回销毁了, 但还是会执行请求成功或失败的block, 导致野指针异常
原本我以为是我程序框架有问题...后来才知道, 无知真可怕... __unsafe_unretained __block typeof(self) weakSelf = self; AFHTTPSes ...
- windows下安装php笔记
为了更深入的理解下服务端的配置 ,上篇文章讲了如何在windows下安装apache , 接下来再研究下在windows下安装php并且结合apache运行php环境, 纯属学习用 ^^ ,如果嫌麻烦 ...
- uva 482 - Permutation Arrays
<int, double> --> <int, string> 从而避免了输出格式: #include <vector> #include <strin ...
- Bash shell 笔记总结(一) 转自http://www.bubuko.com/infodetail-509992.html,谢谢原作者
本文是上课笔记总结,涉及细节知识点会在以后文章说明! bash脚本编程: 脚本程序:解释器解释执行: shell: 交互式接口:编程环境: shell: 能够提供一些内部命令,并且能通过PATH环境变 ...
- KVM virt-manager使用.
本来不想写,但是觉得教程就应该详细点..所以又有了这篇文章..主要是对图形化kvm管理的一些说明 接着上一篇... 1.Virtual Machine Manager 摘要: 打开Virtual Ma ...