Sort a linked list using insertion sort.

class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
ListNode *result;
result->val = INT_MIN;
result->next = NULL;
ListNode *cur=head,*pos,*pre;
while(cur!=NULL)
{
pos = result->next;
pre = result;
while(pos != NULL && pos->val <= cur->val)
{
pre = pos;
pos = pos->next;
}
ListNode *temp = cur->next;
pre->next = cur;
cur->next = pos;
cur = temp;
}
return result->next;
}
};

leetcode——Insertion Sort List 对链表进行插入排序(AC)的更多相关文章

  1. Leetcode147. Insertion Sort List对链表进行插入排序

    对链表进行插入排序. 从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示). 每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中. 插入排序算法: 插入排序是 ...

  2. LeetCode——Insertion Sort List

    LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...

  3. [LeetCode] Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. 链表的插入排序实现原理很简单,就是一个元素一个元素的从原链表中取出来,然后按顺序插入到新链表中,时间复杂度为O(n2) ...

  4. LeetCode :: Insertion Sort List [具体分析]

    Sort a linked list using insertion sort. 仍然是一个很简洁的题目,让我们用插入排序给链表排序:这里说到插入排序.能够来回想一下, 最主要的入门排序算法.就是插入 ...

  5. 9. Sort List && Insertion Sort List (链表排序总结)

    Sort List Sort a linked list in O(n log n) time using constant space complexity.                   H ...

  6. [leetcode]Insertion Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科 ...

  7. leetcode Insertion Sort List

    题目:Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct Li ...

  8. LeetCode: Insertion Sort List 解题报告

    Insertion Sort List Sort a linked list using insertion sort. SOLUTION: 使用一个dummynode 创建一个新的链,将旧的节点插入 ...

  9. The insertion sort algorithm expressed in pseudocode - 插入排序

    Computer Science An Overview _J. Glenn Brookshear _11th Edition procedure Sort (List) N ← 2; while ( ...

随机推荐

  1. nignx 502错误不能使用/的路径方式 即pathinfo

    在server中加入 include enable-php-pathinfo.conf; 引入nginx.conf下的这个文件即可. 如果是tp框架,主要隐藏index.php的入口文件,再加入下面这 ...

  2. ASP.NET-Microsoft.Management.Infrastructure错误

    错误如图所示,将MVC发布到IIS上就会出现这个错误,我用到了NPOI这个EXCEL插件,不知道是不是这个造成的,但是实在找不到解决方案,就直接将BIN目录下的这个Microsoft.Manageme ...

  3. Django连接mysql

    链接文档地址:https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial02/ 由于我使用的是mysql,所以设置的是mysql的: 在mysl ...

  4. NOIP2017提高组模拟赛 7(总结)

    NOIP2017提高组模拟赛 7(总结) 第一题 斯诺克 考虑这样一个斯诺克球台,它只有四个袋口,分别在四个角上(如下图所示).我们把所有桌子边界上的整数点作为击球点(除了4个袋口),在每个击球点我们 ...

  5. 采集电脑摄像头和mic,rtp端口推送音视频工具

    介绍:这个是我在做一个rtmp播放的项目中自己写的rtp推送的工具,可选择摄像头,可选择推送rtp的端口和ip 下载地址: github:https://github.com/alexhegang/s ...

  6. C#post调用接口并上传文件

    /// <summary> /// C#调用接口上传json数据,并且带文件上传 /// </summary> /// <param name="url&quo ...

  7. iOS系统的特点-iOS为什么运行更流畅

    1.进程管理机制-不允许后台进程: 2.用户事件响应优先级: 3.GPU加速: 4.系统内存管理机制: 5.运行机制-机器码直接运行-非虚拟机.

  8. Thread-local storage

    Thread-local storage (TLS) is a computer programming method that uses static or global memory local ...

  9. 前端压缩图片,前端压缩图片后转换为base64.

    今天利用一上午研究了一下前端如何将5m左右的照片转换base64大小为 100k以内! 有两个链接:https://www.cnblogs.com/007sx/p/7583202.html :http ...

  10. vue循环遍历给div添加id

    html部分 <div class="img-preview" v-for="(img,i) of list" :key="img.imageK ...