leetcode Insertion Sort List
题目:Sort a linked list using insertion sort.
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *insertionSortList(ListNode *head) { if (head==NULL||head->next==NULL)//only 0,1nodes
{
return head;
} ListNode *pNode=head->next,*hNode=head,*tNode=NULL;
hNode->next=NULL;
while(pNode)
{
hNode=head; // find where to insert
while (hNode)
{
if (hNode->val>pNode->val)//插入头结点之前
{
tNode=pNode->next;
pNode->next=hNode;
head=pNode;
hNode=head;
pNode=tNode;
break;
}
else if (hNode->next==NULL)//到了最后一个节点
{
tNode=pNode->next;
hNode->next=pNode;
pNode->next=NULL;
pNode=tNode;
break;
}
else if (hNode->next->val>pNode->val)
{
tNode=pNode->next;
pNode->next=hNode->next;
hNode->next=pNode;
pNode=tNode;
break;
}
else
{
hNode=hNode->next;
} } } return head;
}
};
leetcode Insertion Sort List的更多相关文章
- LeetCode——Insertion Sort List
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...
- [LeetCode] Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...
- LeetCode :: Insertion Sort List [具体分析]
Sort a linked list using insertion sort. 仍然是一个很简洁的题目,让我们用插入排序给链表排序:这里说到插入排序.能够来回想一下, 最主要的入门排序算法.就是插入 ...
- LeetCode: Insertion Sort List 解题报告
Insertion Sort List Sort a linked list using insertion sort. SOLUTION: 使用一个dummynode 创建一个新的链,将旧的节点插入 ...
- leetcode——Insertion Sort List 对链表进行插入排序(AC)
Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...
- [leetcode]Insertion Sort List @ Python
原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...
- 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] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
随机推荐
- Java C# C语言中的占位符
一般拼接一段字符串在编程中是很常见的事,下面简单做个总结: 什么是占位符?占位符就是先占住一个固定的位置,等着你再往里面添加内容的符号. 1.Java中处理方法: package com.amos; ...
- 小程序de 一些经验1
尝试着写微信的小程序,一个简单的表单验证.一开始就花了大把的时间尝试如何开始小程序的准备工作. 鼓捣半天,AppId是没有的,于是用了不用appId的模拟版.其实只要下载一个小程序版的微信开发工具. ...
- sql一对多的两个表的update
scie_apprecord仪器表 和 scie_apporder仪器预约时间表 ,一个仪器可以有多条预约时间. 仪器表: 预约时间表: 需求: 由于一个仪器有好多条预约记录,将预约时间表的最 ...
- yarn container启动失败
在yarn资源管理的集群上运行spark程序,无法读取的数据多与少,都会报这个错误,但是其他程序在集群上能够正常运行. 16/11/14 00:13:44 WARN cluster.YarnSched ...
- git分支管理一
1.创建本地分支 local_branch git branch local_branch 2.创建本地分支local_branch 并切换到local_branch分支 git checkout - ...
- 在Visual Studio 2012 Blue theme下使用Dark theme的文本编辑器颜色设置
Visual Studio 2012 默认提供了3种color theme: blue,light,和dark.其中dark的文本编辑器颜色设定很爽,可是整个菜单项加上一些小的窗口如Find Resu ...
- 「脑洞」图片转HTML(支持动画)
也许是受到很久以前看到的这玩意儿的原因:The Shapes of CSS 现在开脑洞写了个自动转换,顺便支持了动画……嗯,纯 CSS (:з」∠) 主要步骤就是用 Python 读图片,然后把像素全 ...
- dos2unix unix2dos
实现windows和linux之间的文件自动转换,消除^M.
- 数据仓储之DLL层接口设计
一.接口设计 1.1. IBaseRepository.cs public interface IBaseRepository<T> { T Add(T entity); bool Upd ...
- Python3 升级pip
(Windows) 用 pip install --Upgrade pip进行自升级不成功,执行至卸载完再安装时出错,最后还是用get_pip.py解决了!