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. EF使用笔记

    最近写接口导数据到大数据中心,但是实体数据字段非常多,如果手动去建数据库表和写插入语句效率非常低,所以想都试一试EF,效率之高,简直吓人,所以此文详细记录操作过程以备下次使用时之用.仅需六部就可完成建 ...

  2. PHP 传值操作和传地址操作

    PHP中有两种赋值操作 一种是传值操作 example: $a = 1; $b = $b; 这个就是传值 传地址是: example2: $a = 1; $b = &$a; 这两者有什么区别呢 ...

  3. WPScan扫描Wordpress漏洞

    一.什么是Wpscan?什么是Wordpres? 1.Wpscan WPScan是一个扫描WordPress漏洞的黑盒子扫描器,可以扫描出wordpress的版本,主题,插件,后台用户以及爆破后台用户 ...

  4. python ddt模块

    ddt模块包含了一个类的装饰器ddt和两个方法的装饰器: data:包含多个你想要传给测试用例的参数: file_data:会从json或yaml中加载数据: 通常data中包含的每一个值都会作为一个 ...

  5. 根据字体多少使UILabel自动调节尺寸

    原文:http://blog.csdn.net/enuola/article/details/8559588 在大多属性情况下,给UILabel进行动态数据绑定的时候,往往需要根据字符串的多少,动态调 ...

  6. [技术] OIer的C++标准库 : 字符串库

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...

  7. BZOJ4245:[ONTAK2015]OR-XOR(贪心)

    Description 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费用为c[1] or c[2] or ...

  8. c++ ActiveX基础1:使用VS2010创建MFC ActiveX工程项目

    1.ActiveX的基本概念 ActiveX控件可以看作是一个极小的服务器应用程序,它不能独立运行,必须嵌入到某个容器程序中,与该容器一起运行.这个容器包括WEB网页,应用程序窗体等... Activ ...

  9. Node.js实战(四)之调试Node.js

    当项目逐渐扩大以后,功能越来越多,这时有的时候需要增加或者修改,同时优化某些功能,就有可能出问题了.针对于线上Linux环境我们应该如何调试项目呢? 别怕,Node.js已经为我们考虑到了. 通过 n ...

  10. 矿难让显卡压了那么多货咋办?NV如是说

    在苏州 GTC 开幕的几天前,英伟达刚刚遭遇了一次股价的腰斩. 近来加密货币的热度渐低,受到挖矿热潮照顾许多的英伟达「矿机」销量受到打击,甚至出现了严重的库存危机,加上近来刚刚发的 RTX20 系列显 ...