LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort.
Subscribe to see which companies asked this question
解答
对于链表的插入排序,用tmp_tail遍历链表,每次的待插入数是tmp_tail->next的元素,待插入数必须从头开始比较,当然从头开始比较时要注意处理待排序数小于或等于链表首元素的情况,因为插入在链表的首元素之前与一般情况的插入不同,而如果待插入数插入在它之前的数列中,则用于遍历链表的指针tmp_tail不需要移动,这与数组不同,毕竟数组插入时是要将部分数组元素整体移动的,而链表不需要,将待插入数插入在前面的链表中后tmp_tail的下一个数就是待排序数,但是如果待插入数保持原位不动,则需要将tmp_tail后移一个,因为新的待插入数是原先待插入数的下一个数,判断待插入数是否在原位只需要在遍历结束后判断tmp_tail->next是否等于tmp_node(待插入数)即可。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertionSortList(struct ListNode* head) {
struct ListNode *tmp_node, *tmp_head, *tmp_tail = head;
if(NULL == tmp_tail){
return head;
}
while(NULL != tmp_tail->next){
tmp_node = tmp_tail->next;
if(head->val >= tmp_node->val){
tmp_tail->next = tmp_node->next;
tmp_node->next = head;
head = tmp_node;
}
else{
tmp_head = head;
while(tmp_node != tmp_head->next){
if(tmp_head->next->val < tmp_node->val){
tmp_head = tmp_head->next;
}
else{
tmp_tail->next = tmp_node->next;
tmp_node->next = tmp_head->next;
tmp_head->next = tmp_node;
break;
}
}
if(tmp_tail->next == tmp_node){
tmp_tail = tmp_tail->next;
}
}
}
return head;
}
LeetCode OJ 147. Insertion Sort List的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode OJ】Insertion Sort List
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * D ...
- 【leetcode】147. Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...
- LeetCode OJ:Insertion Sort List (插入排序链表)
Sort a linked list using insertion sort. 用插入排序来排序一个list,额, 我写的好麻烦啊, debug了好久,至少提交了5次...写吐了快,先贴代码,写的也 ...
- 【刷题-LeetCode】147 Insertion Sort List
Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- 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 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
随机推荐
- [poj 1502]昂贵的聘礼
一道不算太难的最短路喵~ 容我吐槽一下,酋长的地位居然不是最高的额——那你特么的居然还算是酋长?! 枚举一个地位区间 [i..i+M-1] 只要所有的交易者的地位都在该区间中,那么就不会引起冲突 而这 ...
- 【转载】开发备必:WEB前端开发规范文档
规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必 须按本文档规范进行前台页面开发. 本文档如有不对或者 ...
- MindProject R6025解决
-- 来之百度贴吧,收藏在此. 不知道吧里有没有人用mindmanager2012,如果用的话会发现保存.mmap文件后,移动到其他地方,再打开,就会出错,软件关闭.网上很多人说是模版里tips的原因 ...
- post 与 get 在转码的区别
前端输入中文的时候,后端post通过 String text = getRequest().getParameter("text");可以正常拿到中文, 但是通过get的时候就会出 ...
- nova boot instance call flow
参考http://www.cnblogs.com/popsuper1982/p/3927390.html
- android学习笔记51——SQLite 手势Gesture
手势Gesture 所谓手势,是指用户手指或触摸笔在触摸屏幕上的连续触碰行为. Androi对两种手势行为都提供了支持: 1.对于第一种手势而言,android提供了手势检测,并为手势检测提供了相应的 ...
- javascript性能优化总结二(转载)
上面一篇文章大致介绍了一些javascript当中使用的一些小技巧,当下这篇文章继续介绍一下内存管理.松散耦合.性能方面的一些小知识.为避免错误应该注意的点 内存管理 1.循环引用 如果循环引用中包含 ...
- Javascript Promise对象学习
ES6中的Promise对象 var p = new Promise(function(resolve, reject){ window.setTimeout(function(){ console. ...
- archlinux 内核编译笔记
# cp linux-3.10.5.tar.gz /usr/src/linux-3.10.5.tar.gz# cd /usr/src# tar xvzf linux-3.10.5.tar.gz lin ...
- 删除空文件夹 清除CS扩展名文件 bat
删除空文件夹.删的干净.删的彻底. 将下列代码复制到txt中保存.并把后缀.txt命成.bat.然后运行即可. 方案1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...