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. NYIST 860 又见01背包

    又见01背包时间限制:1000 ms | 内存限制:65535 KB难度:3 描述 有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W 的物品,求所有挑选方案中物品价值总 ...

  2. COGS——T 438. 烦人的幻灯片

    http://www.cogs.pro/cogs/problem/problem.php?pid=438 ★☆   输入文件:slides.in   输出文件:slides.out   简单对比时间限 ...

  3. [AngularJS]Chapter 5 与服务器交互

    第八章有关于缓存的东西. [通过$http交互] 传统的AJAX请求如下 var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange ...

  4. MySQL优化之——集群搭建步骤具体解释

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46833179 1 概述 MySQL Cluster 是MySQL 适合于分布式计算 ...

  5. [jQuery] 选择器和事件

    jQuery选择器 属性选择器 <p>p1</p> <span style="font-size:24px;"></span>< ...

  6. crm操作报价单实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages;     using Microsoft ...

  7. Authentication in asp.net

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/introduction/an-o ...

  8. 仿写从iOS8开始支持的UIAlertController:BGAAlertController-Android

    工作以来公司UI设计师出的Android效果图都是iOS风格的UIAlertView和UIActionSheet,新项目还是用原来那一套,不想重复造轮子,所以仿写了从iOS8开始支持的UIAlertC ...

  9. NVMe到底是什么?

    转:http://www.expreview.com/42142.html 有关注SSD的朋友应该今年听到NVMe这个词的频率应该不低,随着高端SSD的战场已经抛弃SATA向PCI-E转移,老旧的AH ...

  10. Hadoop编译源码

    Hadoop编译源码 克隆一个虚拟机 然后一步一步安装就行 安装所需:链接: https://pan.baidu.com/s/1jIZlQmi 密码: gggv 5.1 前期准备工作 1)CentOS ...