Leetcode147. Insertion Sort List对链表进行插入排序
对链表进行插入排序。
从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。
每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。
插入排序算法:
- 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
- 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
- 重复直到所有输入数据插入完为止。
示例 1:
输入: 4->2->1->3 输出: 1->2->3->4
示例 2:
输入: -1->5->3->4->0 输出: -1->0->3->4->5
class Solution {
public:
ListNode* insertionSortList(ListNode* head)
{
if(head == NULL)
return NULL;
ListNode *node = head ->next;
ListNode *newhead = new ListNode(0);
newhead ->next = head;
head ->next = NULL;
while(node != NULL)
{
ListNode *last = newhead;
ListNode *cur = newhead ->next;
ListNode *nextNode = node ->next;
bool isInsert = false;
while(cur != NULL)
{
if(node ->val > cur ->val)
{
last = cur;
cur = cur ->next;
}
else
{
isInsert = true;
last ->next = node;
node ->next = cur;
break;
}
}
if(!isInsert)
{
last ->next = node;
node ->next = NULL;
}
node = nextNode;
if(nextNode != NULL)
nextNode = nextNode ->next;
}
return newhead ->next;
}
};
Leetcode147. Insertion Sort List对链表进行插入排序的更多相关文章
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- LeetCode147:Insertion Sort List
题目: Sort a linked list using insertion sort. 解题思路: 按题目要求,直接进行插入排序 实现代码: #include <iostream> us ...
- The insertion sort algorithm expressed in pseudocode - 插入排序
Computer Science An Overview _J. Glenn Brookshear _11th Edition procedure Sort (List) N ← 2; while ( ...
- LeetCode(147) Insertion Sort List
题目 Sort a linked list using insertion sort. 分析 实现链表的插入排序 注意: 程序入口的特殊输入判断处理! 节点的链接处理,避免出现断链! AC代码 /** ...
- [Swift]LeetCode147. 对链表进行插入排序 | Insertion Sort List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- insertion sort list (使用插入排序给链表排序)
Sort a linked list using insertion sort. 对于数组的插入排序,可以参看排序算法入门之插入排序(java实现),遍历每个元素,然后相当于把每个元素插入到前面已经排 ...
- Insertion Sort List——链表的插入排序
Sort a linked list using insertion sort. 这道题跟 Sort List 类似,要求在链表上实现一种排序算法,这道题是指定实现插入排序.插入排序是一种O(n^2) ...
随机推荐
- <day005>jQuery事件、文档基本操作 + 点赞事件
任务1: jQuery的基本操作 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta cha ...
- Foundation框架系列-NSArray
NSArray常用API 数组字符串指定字符拼接 // 将数组中的元素以separator拼接返回字符串 比如@[@"a=1", @"b=2"] 以separa ...
- JS数组 二维数组 二维数组的表示 方法一: myarray[ ][ ];方法二:var Myarr = [[0 , 1 , 2 ],[1 , 2 , 3, ]]
二维数组 一维数组,我们看成一组盒子,每个盒子只能放一个内容. 一维数组的表示: myarray[ ] 二维数组,我们看成一组盒子,不过每个盒子里还可以放多个盒子. 二维数组的表示: myarray[ ...
- 输出内容 document.write() 可用于直接向 HTML 输出流写内容。简单的说就是直接在网页中输出内容
输出内容(document.write) document.write() 可用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容. 第一种:输出内容用""括起,直 ...
- display: -webkit-box; 做个小小试验
最近做个微信项目发现css3在微信内部浏览器中和其他浏览有些区别 做个小小笔记 .job { display: -webkit-box; display: flexbox; -webkit-box-p ...
- 宽域POST提交数据
小数据宽域可以使用jsonp,但是大数据跨域必须post那么有以下2种方式 1,传统方式 动态生成form var url = ''var $iframe = $("<iframe s ...
- 继承Activity和View
1,当你自定一个继承自view的视图A之后, 如果你在一个继承自Activity的组件B中需要使用A里面的一些方法,如果在B中需要使用A中的一些方法好像不可以直接使用. 需要在B中使用setConte ...
- Codeforces Parking Lot
http://codeforces.com/problemset/problem/630/I 简单的排列组合,推式子技巧:举一个小样例,看着推,别抽象着推,容易错 #include <iostr ...
- strict
strict为3.2.3新增连贯操作,用于设置数据写入和查询是否严格检查是否存在字段.默认情况下不合法数据字段自动删除,如果设置了严格检查则会抛出异常. 例如: $model->strict(t ...
- 逻辑回归代码demo
程序所用文件:https://files.cnblogs.com/files/henuliulei/%E5%9B%9E%E5%BD%92%E5%88%86%E7%B1%BB%E6%95%B0%E6%8 ...