Sort a linked list using insertion sort.

链表的插入排序。

需要创建一个虚拟节点。注意点就是不要节点之间断了。

class Solution {
public: ListNode* insertionSortList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* newhead = new ListNode(-);
newhead->next=head;
ListNode* q=head;
ListNode* p=head->next;
while(p!=NULL){
ListNode* s1=newhead;
ListNode* s2=s1->next;
while(s2!=p&&s2->val<=p->val){
s1=s2;
s2=s2->next;
}
if(s2!=p){
s1->next=p;
q->next=p->next;
p->next=s2; p=q->next;}
else{
q=p;
p=p->next;
}
}
return newhead->next;
}
};

【leetcode】147. Insertion Sort List的更多相关文章

  1. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. 【刷题-LeetCode】147 Insertion Sort List

    Insertion Sort List Sort a linked list using insertion sort. A graphical example of insertion sort. ...

  3. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  4. 【leetcode】1122. Relative Sort Array

    题目如下: Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 ar ...

  5. 【LeetCode】280. Wiggle Sort 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...

  6. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  7. 【leetcode】280.Wiggle Sort

    原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...

  8. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. 把对象列表转化成json数据格式

    package JsonTest; import java.util.ArrayList; import java.util.List; public class test { public stat ...

  2. Python3 常用数据类型语法

    1.int类型 int类型的数据是没有长度限制的,它的最大长度只与计算机的内存有关. bin(i)      返回二进制表示结果, hex(i)      十六进制, int(i)       整数( ...

  3. H3CNE实验:配置VLAN和VLAN端口

    配置准备数据: | 设备名称 | IP地址 | VLAN网关 | 接口 | VLAN | |---------------|--------------|----------------|------ ...

  4. VB6之GIF分解

    原文链接:http://hi.baidu.com/coo_boi/item/1264a64172fe8dec1f19bc08 还是找了个C++的翻译下,原文链接:http://www.360doc.c ...

  5. 解决win10系统以太网适配器的驱动程序可能出现问题

    插上网线显示未连接-连接可用,连上无线显示未连接-连接不可用,右下角显示感叹号 ,以太网和无线属性显示ipv4未连接详细信息为空,在设备管理器里卸载网卡驱动重装上仍然没有,通过windoes自带的网络 ...

  6. Luogu 1060 开心的金明 / NOIP 2006 (动态规划)

    Luogu 1060 开心的金明 / NOIP 2006 (动态规划) Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨 ...

  7. Es6 新增函数

    ====函数的扩展 -----ES6 之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log( ...

  8. docker~使用阿里加速器

    回到目录 国外的docker hub速度慢这是公认的,而我们可以使用阿里提供的加速器,管理你的镜像,拉别人的镜像等等. 注册一个阿里的账号 进行加速器页面https://cr.console.aliy ...

  9. 回味Python2.7——笔记3

    一.错误和异常 1.异常处理 >>> while True: ... try: ... x = int(raw_input("Please enter a number: ...

  10. TensorFlow conv2d原理及实践

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None) 官方 ...