Description

Sort a linked list using insertion sort.

Example

Given 1->3->2->0->null, return 0->1->2->3->null.

解题:用插入法排序链表。很简单的一道题目,但还是出现了很多问题。

总结一下遇到的问题:

(1)没有头结点的情况下,为了方便可以构造一个,返回头结点的next就行了。

(2)没有必要一直在原来的链表上纠结,完全可以申请一个头结点,将原链表结点一个个插进去。

(3)没有必要一上来就考虑怎样写更简单,能做出来才是最重要的,写好了一种方法后可以再优化。

(4)多组测试数据没通过后,要考虑一下算法的可行性,别在一个坑里爬不出来。

代码如下:

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param head: The first node of linked list.
* @return: The head of linked list.
*/
public ListNode insertionSortList(ListNode head) {
// write your code here
//新链表的头结点
ListNode dummy = new ListNode(0);
while(head != null){
ListNode node = dummy;
while(node.next != null && node.next.val < head.val){
node = node.next;
}
ListNode temp = head.next;
head.next = node.next;
node.next = head;
head = temp;
}
return dummy.next;
}
}

173. Insertion Sort List【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  3. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  4. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

  5. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  6. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  7. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  8. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  9. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

随机推荐

  1. 最短路径Dijkstra matlab

    Dijkstra: function [dist,pre, full_path]=MinRoad_Dijkstra(G,v0) n=0; if isfield(G,'w') && ~i ...

  2. scss 使用

    SCSS 常用功能 https://www.cnblogs.com/guangzan/p/10547335.html 定义变量$my-color: #666; //定义变量$my-heihgt: 10 ...

  3. Hive 整合Hbase

    摘要 Hive提供了与HBase的集成,使得能够在HBase表上使用HQL语句进行查询 插入操作以及进行Join和Union等复杂查询.同时也可以将hive表中的数据映射到Hbase中.     应用 ...

  4. vue 文本比较插件

    npm install codemirror diff-match-patch diff-match-patch template: <div id="view">&l ...

  5. 编程算法 - 左旋转字符串 代码(C)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/37689725 左旋转字符串 代码(C) 本文 ...

  6. 连接远程数据库ORACLE11g,错误百出!

    客户机中PLSQL DEV访问虚拟机中的ORACLE11g,错误百出! 创建时间: 2017/10/14 18:44 作者: CNSIMO 标签: ORACLE 忙了一下午,只有两个字形容:麻烦!   ...

  7. rpm -qa 查找文件

    系统环境:centos6.6 yum install 安装的文件找不到安装路径,使用whereis和find -name都无效 rpm -qa | grep -i 关键字  查找 rpm -ql fi ...

  8. Properties类和如何操作属性

    Properties类继承关系java.lang.Object  java.util.Dictionary<K,V>      java.util.Hashtable<Object, ...

  9. JAVA框架 Mybaits 动态代理

    一.动态代理: mybaits给咱们提供一套动态代理,我们只需要按他的要求写接口即可,mybatis帮做动态代理,相当于咱们写的接口的实现类.底层通过反射实例化代理对象,通过代理对象调用相应的方法, ...

  10. (二) DRF 视图

    DRF中的Request 在Django REST Framework中内置的Request类扩展了Django中的Request类,实现了很多方便的功能--如请求数据解析和认证等. 比如,区别于Dj ...