Sort a linked list using insertion sort.

插入排序。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) { if( head == null )
return null; ListNode node = head.next;
head.next = null;
ListNode start = head; while( no sdf sd sdfnull ){
ListNode nn = node.next;
ListNode first = start;
if( node.val < first.val){
node.next = first;
start = node;
}else{
while( first.next != null && first.next.val < node.val )
first = first.next;
node.next = first.next;
first.next = node; } node = nn; }
return start; }
}

leetcode 147. Insertion Sort List ----- java的更多相关文章

  1. Java for LeetCode 147 Insertion Sort List

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

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

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

  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. IT公司100题-12-求1+2+…+n

    问题描述: 求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C).   分析: 利用类的静态变量实现: new一含有n ...

  2. IT公司100题-2-设计带min函数的stack

    问题描述: 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素. 要求函数min.push 以及pop 的时间复杂度都是O(1).   双倍空间实现: 保存2个栈,分别是元素和当前最小值 ...

  3. JS图片加载失败显示默认图片

    代码如下: <div id='photo<%# Container.DataItemIndex+1%>' style="position: absolute; displa ...

  4. python几大排序算法

    1.插入排序 原理:有数列[k1,k2,k3...],假设k1是排好序的,插入k2,排序完成,然后再插入k3,以此类推 def insert_sort(arr): for i in range(1,l ...

  5. ASP.NET是如何在IIS下工作的[转]

    ASP.NET与IIS是紧密联系的,由于IIS6.0与IIS7.0的工作方式的不同,导致ASP.NET的工作原理也发生了相应的变化. IIS6(IIS7的经典模式)与IIS7的集成模式的不同 IIS6 ...

  6. JS 基于面向对象的 轮播图2

    <script> // 函数不能重名, --> 子函数 // is defined function --> 函数名是否写错了 function AutoTab(id) { T ...

  7. (转)JSON数据格式和js操作json总结

    原:http://niutuku.com/tech/javaScript/273643.shtml JSON数据格式和js操作json总结 来源:niutuku.com |         vince ...

  8. hdu 2035

    Ps:查了下快速幂,顺便在这用下.... 积的求余等于两个数的求余的积再求余... 代码: #include "stdio.h"int mod(int a,int b);int m ...

  9. Design Patterns----简单的工厂模式

    实例: 实现一个简单的计算器.实现加减乘除等操作.. operator.h 文件 // copyright @ L.J.SHOU Mar.13, 2014 // a simple calculator ...

  10. BZOJ 2393 Cirno的完美算数教室

    就是爆搜嘛. 先从大到小排个序能减去dfs树上很大的一部分.这个技巧要掌握. #include<iostream> #include<cstdio> #include<c ...