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. 【kAri OJ 616】Asce的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 作为一个东北大老爷们,大A熊以力气大著称,现在有一颗半径为r的树,剖面图如黑色的圆,大A熊决定搬几个半径为R的圆柱形桶将其围住,剖面图如 ...

  2. NOIP2014 day2 T2 洛谷P2296 寻找道路

    题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...

  3. Spring3.2.2之后不赞成使用queryForInt

    原来: public int getMatchCount(String username,String password){ String sql="select count(*) from ...

  4. linux内存回收 内核参数

    ss -atu| awk '/^tcp/{++S[$2]} END {for(a in S) print a,S[a]}' ps up pid (RSS:实际内存大小,长驻内存) ps o pid,c ...

  5. 《驾驭Core Data》 第一章 Core Data概述

    <驾驭Core Data>系列教程综合了<Core Data for iOS>,<Learning Core Data for iOS>,<Core Data ...

  6. WebView 自定义错误界面,WebView 加载进度条,和Logding 效果

    ---恢复内容开始--- 下载地址,代码就不粘贴了 http://pan.baidu.com/s/1eQncg86 ---恢复内容结束--- 我没有判断是不是网络原因,各位自行判断吧,图片错误信息,及 ...

  7. 求任意长度数组的最大值(整数类型)。利用params参数实现任意长度的改变。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. ci中如何得到配置的url

    $this->load->helper('url'); 然后,你可以用它查询并返回设置在config.php文件中的site和/或base URL: echo site_url(); ec ...

  9. Java创建目录 mkdir与mkdirs的区别

    两者的参数都是路径串,但: mkdir只能创建父目录存在的目录,而mkdirs不论要创建目录的父目录是否存在都能创建成功. 例如:假设目录c:/uses/zsm/desktop/dir1不存在,,现在 ...

  10. 锋利的jQuery-3--css("height")和.height()的区别

    $("p").css("height") : 获取的高度值与样式的设置有关,可能会得到“auto”, 也可能是字符串“10px”之类的.设置值时如果是数值形式默 ...