Insertion Sort List Leetcode
Sort a linked list using insertion sort.
这个题我巧妙的设置了一个临时头结点
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (head == nullptr)
return head;
ListNode temp();
temp.next = head;
head = &temp;
ListNode *cur = head->next;
while (cur->next != nullptr)
{
ListNode *back = head;
while (back->next != cur->next && back->next->val <= cur->next->val)
back = back->next;
if (back->next != cur->next)
{
ListNode *now = cur->next;
cur->next = cur->next->next;
now->next = back->next;
back->next = now;
}
else {
cur = cur->next;
}
}
return temp.next;
}
};
Insertion Sort List Leetcode的更多相关文章
- Insertion Sort List —— LeetCode
Sort a linked list using insertion sort. 题目大意:将一个单链表使用插入排序的方式排序. 解题思路:先新建一个头指针,然后重新构建一下这个单链表,每次从头找到第 ...
- Insertion Sort List Leetcode java
题目: Sort a linked list using insertion sort. 题解: Insertion Sort就是把一个一个元素往已排好序的list中插入的过程. 初始时,sorted ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- LeetCode解题报告:Insertion Sort List
Insertion Sort List Sort a linked list using insertion sort. leetcode subject思路:标准的插入排序.考察一下链表的操作. 对 ...
- [Leetcode Week16]Insertion Sort List
Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ ...
- LeetCode——Insertion Sort List
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...
- [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 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode Insertion Sort List
题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...
随机推荐
- 使用(Drawable)资源——图片资源
图片资源是最简单的Drawable资源,只要把*.png.*.jpg.*.gif等格式的图片放入/res/drawble-xxx目录下,Android SDK就会在编译应用中自动加载该图片,并在R资源 ...
- HDU1848-Fibonacci again and again
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1848 这个题目代码不是很复杂,但那个等价类,(SG函数)没怎么理解, 题目难,不过代码不怎么复杂,在网 ...
- secureCRT自动化脚本
http://cysnow.iteye.com/blog/1698791 cd \crt "C:\Program Files\VanDyke Software\Clients\SecureC ...
- MySQL锁详解
一.概述 数据库锁定机制简单来说就是数据库为了保证数据的一致性而使各种共享资源在被并发访问访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制,所以MySQL自然也不能例外.My ...
- GreenOpenPaint的实现(一)基本框架
Win7下的MSPaint是Ribbon的典型运行.这种SDI程序对于图像处理来说是很适合的.利用春节时间,基于之前的积累,我实现GreenOpenPaint,主要就是模拟MSPaint的界面,实现出 ...
- Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...
- jquery mobile多页面跳转等,data-ajax="false" 问题,
当我们的网站引用了jquery mobile的js后,点击页面的链接,你会发现页面无法跳转,因为jquery mobile默认是采用ajax方式来加载网站的,如果你需要跳到另一个页面,需要在a标签加上 ...
- vue.js 常用语法总结(一)
作者:曾萍,目前就职于京东商城. 概述 2016年已经结束了.你是否会思考一下,自己在过去的一年里是否错过一些重要的东西?不用担心,我们正在回顾那些流行的趋势.通过比较过去12个月里Github所增加 ...
- 制作 OpenStack Linux 镜像 - 每天5分钟玩转 OpenStack(151)
这是 OpenStack 实施经验分享系列的第 1 篇. OpenStack 的 instance 是通过 Glance 镜像部署的,所以准备镜像是必须要做的工作.本节介绍 Linux 镜像的制作方法 ...
- 安卓作为udp服务器,PC作为客户端,仅监听
安卓客户端作为udp服务器,监听其他客户端的数据,测试已成功 本次实验所用数据: 安卓作为服务器: 端口:8888 IP:192.168.1.104 电脑作为客户端: 端口:50 ...