Sort a linked list using insertion sort.

解题思路:

插入排序,JAVA实现如下:

    public ListNode insertionSortList(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode root=new ListNode(Integer.MIN_VALUE);
root.next=head;
head=head.next;
root.next.next=null;
ListNode temp=root,temp2=root;
L1:while(head!=null){
temp=root;
while(head.val>temp.next.val){
temp=temp.next;
if(temp.next==null){
temp.next=head;
head=head.next;
temp.next.next=null;
continue L1;
}
}
temp2=head;
head=head.next;
temp2.next=temp.next;
temp.next=temp2;
}
return root.next;
}

Java for LeetCode 147 Insertion Sort List的更多相关文章

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

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

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

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

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

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

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

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

  5. Leetcode#147 Insertion Sort List

    原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...

  6. [leetcode] 147. Insertion Sort List (Medium)

    原题 别人的思路 非常简洁 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} h ...

  7. [LeetCode]147. Insertion Sort List链表排序

    插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...

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

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

  9. LeetCode OJ 147. Insertion Sort List

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

随机推荐

  1. Mysql数据库登录问题:Your password has expired.

    ERROR 1862 (HY000): Your password has expired. To log in you mustchange it using a client that suppo ...

  2. 39.Android版本小知识

    中文名----英文名----版本----对应API Level 棉花糖 Marshmallow - 6.0.1_r10 - API 23棉花糖 Marshmallow - 6.0.0_r5 - API ...

  3. javaScript基础练习题-下拉框制作

    1.基础回顾 如何让一个段javascript在文档加载后执行,(因为自己忘了,所以顺便复习一下) window.onload = function(){}; <!DOCTYPE html PU ...

  4. Spring学习8- SSH需要的jar包

    struts2 commons-logging-1.0.4.jar 主要用于日志处理 freemarker-2.3.8.jar 模板相关操作需要包 ognl-2.6.11.jar ognl表达示所需包 ...

  5. XUnit学习

    1.建立测试单元项目 2.引用XUnit.dll或者在Nuget里安装XUnit 3.安装Nuget->xUnit.net[Runner: Visual Studio] 4.打开 测试-> ...

  6. 通过telnet连接查看memcache服务器(转)

    memcache作为一款优秀的进程外缓存,常常被运用于高并发系统架构中.这里主要谈谈怎么通过telnet工具,查看memcache运行状况并对其key进行管理维护.假设memcache安装目录:/us ...

  7. ASP.NET MVC 开启AJAX跨域请求

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  8. TCP,IP,HTTP,SOCKET区别和联系

    物理层-- 数据链路层-- 传输层--                       TCP协议 会话层-- 我 们在传输数据时,可以只使用(传输层)TCP/IP协议,但是那样的话,如 果没有应用层,便 ...

  9. 不使用配置文件动态注册HttpModule

    在asp.net 4.0中,提供了一种不通过修改配置文件注册Module的方法.从.net3.5开始,新提供的PreApplicationStartMethodAttribute特性可以应用在程序集上 ...

  10. C函数tolower,与toupper

    tolower    将大写转换成小写.  非字母字符不做出处理.   这个函数用法有点特殊他是处理字符的,而不是处理字符串的. 所谓的不能处理字符串不是说他不能处理字符串,他处理的时候对字符串必须是 ...