LeetCode OJ-- Insertion Sort List **
https://oj.leetcode.com/problems/insertion-sort-list/
链表实现插入排序
首先插入排序是指:
a b c d e g m 对b也就是第二个位置选做元素num开始遍历n-1次
对每个num,找到从第0个位置开始,它应该待的位置,把它插入进去。
对于链表来说,首先new一个 dummy 节点,这样比较方便对 head的操作 dummy->next = head。
然后从head->next为第二个元素,也就是从它开始遍历处理
每一次处理中从 dummy->next作为开始,因为 head 可能已经换位置了.
记录当前处理的位置的上一个位置,这样如果需要把这个点插入到前面,也就是从这个位置删除的话,就 before->next = num->next相当于删除了。
同时为了插入,即在合适的位置插入,需要记录要插入的上一个位置 num->next = pointer, before_should->next = num,相当于插入了。
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL)
return NULL;
ListNode *dummy = new ListNode(-);
dummy->next = head;
ListNode *before = head;
ListNode *before_bigger = dummy;
for(ListNode *num = head->next; num!=NULL; num = num->next)
{
ListNode *point = NULL;
bool flag = false;
before_bigger = dummy;
for(point = dummy->next; point!= num; point=point->next)
{
if(point->val > num->val)
{
flag = true;
break;
}
before_bigger = before_bigger->next;
}
//need insert
if(flag)
{
before->next = num->next;
before_bigger->next = num;
num->next = point;
num = before;
}
else
{
before = before->next;
}
}
return dummy->next;
}
};
LeetCode OJ-- Insertion Sort List **的更多相关文章
- [Leetcode Week16]Insertion Sort List
Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ ...
- leetcode 【 Insertion Sort List 】 python 实现
题目: Sort a linked list using insertion sort. 代码:oj测试通过 Runtime: 860 ms # Definition for singly-linke ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 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:Insertion Sort List
Sort a linked list using insertion sort. 分析:此题要求在链表上实现插入排序. 思路:插入排序是一种O(n^2)复杂度的算法,基本想法就是每次循环找到一个元素在 ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- leetcode 名单 Insertion Sort List
Insertion Sort List Total Accepted: 24444 Total Submissions: 96639My Submissions Sort a linked 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 ...
随机推荐
- du 与df 统计系统磁盘不一致原因与解决方法
事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...
- 使用 Sconfig.cmd 配置服务器核心服务器
使用 Sconfig.cmd 配置服务器核心服务器 适用对象:Windows Server 2012 R2, Windows Server 2012 在 Windows Server 2012 中,你 ...
- Careercup - Microsoft面试题 - 5799446021406720
2014-05-12 07:17 题目链接 原题: Given below is a tree/trie A B c D e F a<b<e<>>c<>d&l ...
- vmware克隆centos修改linux mac地址
故障背景: 在vmware workstation中了完全克隆了一个已经存在的centos的虚拟机,启动之后发现网卡没有启动.于是重启一下network服务,发现提示错误信息“Device eth0 ...
- 使用 htaccess 重写 url,隐藏查询字符串
例如我们有如下 URL: http://example.com/users.php?name=tania 但是我们想要让 URL 变成如下: http://example.com/users/tani ...
- LeetCode 62 不同路径
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角.问总共有多少条不同的路径? 示例 1: 输入: ...
- (转载) Linux五种IO模型
转载:http://blog.csdn.net/jay900323/article/details/18141217 Linux五种IO模型及分析 目录(?)[-] 概念理解 Linux下 ...
- RNQOJ Jam的计数法
题目:https://www.rqnoj.cn/problem/3 非递归做法:(严格递增 单调大于 不可等于 ) 做法:循环体 <1>操作字符串 str 从后往前找,k=1,如果s[w ...
- MySql数据库 - 1.安装
下载: 官网:www.mysql.com 打开官网之后依次点击:DOWNLOADS - Windows - MySql Installer MySql Installer 包含的功能,使用C#连接数据 ...
- python发送给邮件 转
这里用到了Python的两个包来发送邮件: smtplib 和 email . Python 的 email 模块里包含了许多实用的邮件格式设置函数,可以用来创建邮件“包裹”.使用的 MIMEText ...