[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. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
随机推荐
- CVPR读书笔记[5]:Gabor特征提取之Gabor核的实现
朱金华 jinhua1982@gmail.com 2014.08.09 本文參考http://blog.csdn.net/njzhujinhua/article/details/38460861 ...
- [AngularJS] Services, Factories, and Providers -- Service vs Factory
Creating a Service: Before actual create an angular service, first create a constructor in Javascrip ...
- 使用symbolicatecrash分析crash文件
对于我们iOS开发者来说,最心碎的事莫过于苹果审核一个星期后上架app store,而第二天就报出闪退bug.一周前我刚经历过,而且最坑的是由于第一次做个人开发,经验不足,没有集成友盟的分析SDK,还 ...
- SWFObject文件上传使用记录
SWFObject文件上传使用方法记录,该插件使用起来相当强大也很灵活,与uploadify各有千秋. 值得一说的是,如果要设置button_image_url这个参数,该参数是按钮的背景图,但是一定 ...
- Android Studio 安装
准备: JDK 7以及以上版本. Android Studio安装文件 中文站下载 http://www.android-studio.org/index.php/download exe ,包含S ...
- Linux命令之 文件归档管理
1.文件相关知识 Linux怎样保存文件 数据 -这里数据就是文件的内容 元数据 -在linux系统中,所有与某个文件相关的额外信息都保存在一个叫做i-节点(inode)的节构中 文件名 -文件名保存 ...
- Error prompt:“wget: unable to resolve host address”---Solution
//Situation System prompts that:"wget: unable to resolve host address". //Analysis Una ...
- (@DBRef)spring-data-mongodb
@DBRef用在哪些地方 已知的有 @DBRefprivate Shop product; @DBRefprivate List<Account> accounts; 如果不加@DB ...
- Error: theForm.submit is not a function !!
theForm.submit is not a function 调试了半天,才发现范了低级错误. 页面中有一个按钮ID 是 submit 而引发的错误. 引出的问题是页面上的元素命名范围不能是 wi ...
- Regex.Escape
C# 字符串变量str 的值为"a\nb"如果直接输出显示的话,就成了:ab需要输出显示为:a\nb问,怎么办?千万别告诉我定义: str=@"a\nb",因为 ...