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 ...
随机推荐
- Kubernetes master服务定制编译docker镜像
前言 之前部署了Kubernetes 1.13.0,发现master服务的启动方式与1.10.4版本有所区别,kube-apiserver.kube-controller-manager和kube-s ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
- Wireshark启动出现“无法启动此程序,因为计算机丢失api-ms-win-crt-runtime-l1-1-0.dll。”
由于重装了win7系统,安装wireshark启动出现了“无法启动此程序,因为计算机丢api-ms-win-crt-runtime-l1-1-0.dll”的问题. 网上查了一圈的资料终解决问题,于是整 ...
- Apache的安装与下载
PHP的运行必然少不了服务器的支持,何为服务器?通俗讲就是在一台计算机上,安装个服务器软件,这台计算机便可以称之为服务器,服务器软件和计算机本身的操作系统是两码事,计算机自身的操作系统可以为linux ...
- 2 Model层-模型成员
1 类的属性 objects:是Manager类型的对象,用于与数据库进行交互 当定义模型类时没有指定管理器,则Django会为模型类提供一个名为objects的管理器 支持明确指定模型类的管理器 c ...
- 【Kubernetes】资源列表
1.Kubernetes资源列表 https://www.cnblogs.com/linuxk/p/10436260.html
- 驱动模块 .ko
模块: 模块机制,作用搞高LINUX操作系统的扩充性. 1. 模块概念: 1.动态可加载内核模块LKM 2.内核空间运行 3.是不是一执行文件,是一个没有经过链接,不能独立运行的一个目标文件(.c-& ...
- 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站
现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...
- C++ STL map容器的说明测试1
// maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...
- 利用python多线程模块实现模拟接口并发
import requestsimport jsonimport threadingimport timeimport uuid class postrequests(): def __init__( ...