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. MySQL主从复制半同步复制原理及搭建

    在MySQL5.5之前的版本中,MySQL的复制是异步复制,主库和从库的数据之间存在一定的延迟,比如网络故障等各种原因,这样子容易存在隐患就是:当在主库写入一个事务成功后并提交了,但是由于从库延迟没有 ...

  2. MySQL索引原理以及类型

    1.什么是索引 索引是在MySQL的存储引擎上,对其表中的某个列或多列通过一些算法实现可快速查询出结果的一种方法. 2.为什么要有索引 就像一本书要有目录一样,我们可快速通过目录来查找对应的章节得出结 ...

  3. MySQL运维之---mysqldump备份、select...into outfile、mysql -e 等工具的使用

    1.mysqldump备份一个数据库 mysqldump命令备份一个数据库的基本语法: mysqldump -u user -p pwd dbname > Backup.sql 我们来讲解一下备 ...

  4. 解决Matlab当中for循环运行慢的问题

    做量化操作的时候经常需要使用到matlab编写策略或者计算多因子,for循环非常慢,自己找了一些matlab中for循环的优化方法,for的部分每处理一个大矩阵都要花费大量的时间,这是不可避免需要遇到 ...

  5. HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. C#/JS AES字符串加密和解密

    往往我们有一种需求:在页面端实现对即将传入到后台端的某些字符串进行加密,然后在后台端对传入进来的字符串做解密.在一些有安全要求的数据传输上会用到此种方式 下面分别列出js端和后台端的加密或解密代码. ...

  7. LNMP搭建 源码包

    LNMP源码包搭建 linux    CentOS-6.5-x86_64-bin-DVD1 nginx   版本1.8.0  下载地址:http://nginx.org/en/download.htm ...

  8. phpstorm开发环境搭建流程

    1.下载phpstorm 2.网上找注册码 phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201 ...

  9. R语言--输入输出

    基本输入输出 输入: readline, edit, fix 输出: print, cat 输出重定向 sink #基本输入输出 x=readline('请输入:') #读取输入,一行为一个字符串 x ...

  10. 解决微云登陆出现wns login error的问题

    原文首发我的主力博客 http://anforen.com/wp/2017/04/weiyunwns_login_error/ PC版微云客户端出现登陆出现wns login error 完全退出QQ ...