Sort a linked list using insertion sort.

class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(!head || !head->next) return head;
ListNode *tail =head->next;
head->next = NULL;
ListNode *current;
ListNode *tmp; while(tail){
if(tail->val < head->val){
tmp = tail->next;
tail->next = head;
head = tail;
tail = tmp;
continue;
} current = head;
while(current->next && tail->val >= current->next-> val) current = current->next;
tmp = tail->next;
tail->next = current->next;
current->next = tail;
tail = tmp;
}
return head;
}
};

147. Insertion Sort List (List)的更多相关文章

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

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

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

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  3. LeetCode OJ 147. Insertion Sort List

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

  4. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  5. 147. Insertion Sort List

    Sort a linked list using insertion sort. 代码如下: /** * Definition for singly-linked list. * public cla ...

  6. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

  7. [LeetCode] 147. Insertion Sort List 解题思路

    Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...

  8. 【leetcode】147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...

  9. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  10. [leetcode sort]147. Insertion Sort List

    Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...

随机推荐

  1. python编程规范系列--建议01~07

    本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本书主要内容     1)容易被忽视的重要概念和常识,如代码的布局和编写函数的原则等:     2)编写py ...

  2. python一些内建函数(map,zip,filter,reduce,yield等)

    python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...

  3. 你一定想知道的关于FPGA的那些事

    首先,如果您从未接触过FPGA(现场可编程门阵列),或者有过一点基础想要继续深入了解这个行业,在这里,会向您介绍FPGA,并且向您解释FPGA都能解决什么问题,如何解决这些问题,并讨论如何将设计进行优 ...

  4. ThreadPoolExecutor之三:自定义线程池-扩展示例

    ThreadPoolExecutor是可扩展的,下面一个示例: package com.dxz.threadpool.demo1; import java.util.concurrent.Blocki ...

  5. json用法常见错误

    Json用法三个常见错误   net.sf.json.JSONException: java.lang.NoSuchMethodException

  6. 一种SequenceFile的格式研究

    最近仔细研究了以下公司中使用的SequenceFile文件格式,SequenceFile的格式比较紧凑,实现了从中间读取文件内容(便于hadoop将文件进行适当地切分),同时也可以支持仅读取文件的元数 ...

  7. 很漂亮的按钮css样式(没有用到图片,可直接拷贝代码使用)

    [转自]http://blog.csdn.net/lushuaiyin/article/details/8118669 对于程序员,有时候也需要对页面风格做些改动,整体的页面风格还是美工的工作. 按钮 ...

  8. 第六章 通过Service访问Pod(上)

    不应该直接使用Pod的ID地址作为对外提供服务的接口,应为一旦Pod重启,IP地址就变化了,解决方案是使用Service. 6.1 创建Service K8s service从逻辑上代表了一组Pod, ...

  9. Angular2快速入门-4.创建一个服务(创建NewsService提供数据)

    上篇我们使用的数据是通过mock-news.ts中的const News[] 数组直接赋给Component 组件的,这篇我们把提供数据的部分单独封装成服务 第一.创建news.service.ts ...

  10. Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(1)

    一.本系列分为6部分 1.Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入 2.Centos 6.5 下安装 Zabbix server 3.0服务器的安 ...