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. 解决Vsphere Client 60天过期问题

  2. 1、Java通过JDBC操作Hive

    0.概述 使用的都是CLI或者hive –e的方式仅允许使用HiveQL执行查询.更新等操作.然而Hive也提供客户端的实现,通过HiveServer或者HiveServer2,客户端可以在不启动CL ...

  3. java web 程序---登陆的验证码实现显示

    是一个java文件 package com.sss; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; im ...

  4. 十六.jQuery源码解析之Sizzle设计思路.htm

    为了便于后面的叙述,需要了解一些相关术语和约定. 并列选择器表达式:"div,p,a"====>div,p,a是并列的. 块表达式:"div>p"中 ...

  5. List、Set、Map下各类型的对比

    1.List和Set: List: 元素有放入顺序,元素可重复,查找效率高,插入删除效率低: Set: 元素无放入顺序,元素不可重复,(元素虽然无顺序,但元素在Set中的位置是由该元素的HashCod ...

  6. OD 快捷键

    F3 为加载一个可执行程序,进行调试分析 F2 为下断点 下完断点后,地址变为红色的 程序执行到断点处的时候会停下来 取消断点也是 F2 F4 为把程序执行到光标所在处 如果光标所在的位置在断点处之后 ...

  7. Django学习---路由url,视图,模板,orm操作

    Django请求周期 url ->  路由系统  ->函数或者类 -> 返回字符串 或者 模板语言 Form表单提交: 点击提交 -> 进入url系统  ->  执行函数 ...

  8. Python 小知识点(8)-- __new__

    第一段代码如下: class Foo(object): def __init__(self,name): self.name = name print("Foo __init__" ...

  9. 新浪微博Oauth2.0授权 获取Access Token

    新浪微博开放平台提供了丰富的API接口,利用这些接口,开发者能够开发出独具特色的微博应用.但是,大部分接口都需要用户授权给应用,应用利用授权得到的Access Token来调用相应的接口来获取内容. ...

  10. numpy的通用函数

    通用函数:快速的元素级数组函数 通用函数是一种对ndarry中的数据执行元素级运算的函数,可以看作是简单函数(接受一个或多个标量值,并产生一个或多个标量值)的矢量化包装器. 一元func: abs丶f ...