`
huntfor
  • 浏览: 195204 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Insertion Sort List

 
阅读更多

新博文地址:[leetcode]Insertion Sort List

 

Insertion Sort List

Sort a linked list using insertion sort.

 用插入排序,对一个单链表进行排序。

插入排序就不多讲了,单链表还是数组,道理都是一样的,只不过单链表略微麻烦一点,好在算法比较简单,难度不大。

     public ListNode insertionSortList(ListNode head) {
    	if(head == null || head.next == null){
    		return head;
    	}
    	ListNode hhead = new ListNode(Integer.MIN_VALUE);
    	hhead.next = head;
    	ListNode begin = head.next;
    	head.next = null;
    	while(begin != null){
    		ListNode tem = hhead;
    		while(tem.next != null){
    			if(tem.next.val < begin.val){
    				tem = tem.next;
    			}else{
    				break;
    			}
    		}
    		if(tem.next != begin){
    			ListNode node = begin.next;
    			begin.next = tem.next;
    			tem.next = begin;
    			begin = node;
    		}else{
    			tem.next = begin;
    			begin = begin.next;
    		}
    	}
    	return hhead.next;
    }   

 

分享到:
评论

相关推荐

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    看leetcode吃力-my-learning-note:大三下学期之资料结构与演算法课程内容与练习

    Insertion Sort List 完成作业Quick sort Week6 10/11 国庆弹性放假 Week7 完成作业Heap sort Week8 完成作业Merge sort Week9 Week10 完成作业Binary Search Tree Week11 Week12 完成作业Hash table Week13 Week14 ...

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode 力码 大批 152-最大乘积子阵列,169-多数元素,189-旋转阵列,198-房屋强盗 二分法 153-在旋转排序数组(II)中查找最小值,162-查找峰值元素 结构 155 分钟堆栈,173 二进制搜索树迭代器,HARD:...

    leetcode答案-leetcode:leetcode

    leetcode 答案 leetcode 08/18 Unique Paths 应该是简单的数学排列组合问题,提炼一下其实就一句话:有m个黑球,n个白球,有多少种不同的排列方式。...Insertion Sort List 在这里遇到前所未遇的惨败——提交了

    leetcode答案-python:Python

    leetcode 答案About Me 我叫做林宏霖,绰号是呱呱。 我喜欢打排球,最喜欢的食物是芒果 我有一个很可爱的女友 Week1 中秋节放假 课程、分数说明 Week2 Linked List Linked list(连结串列)是一种常见的资料结构,其...

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    insertion-sort-list 树 binary-tree-postorder-traversal 树 binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态...

    leetcode530-my-learning-note:我的学习笔记

    第四、五周(InsertionSort、QuickSort) 第六周(Heap Sort) 第七周(Merge Sort) 第八周(Set Mismatch) 第九周(无新进度) 第十周(BST) 第十一周(Hash Table) 第十二、十三周(DFS(Stack) & BFS(Queue)) 第十四、十五周...

    leetcode中文版-ts-datastructures-algorithms:和我一起学算法吧!

    leetcode中文版 本教程是在下从零入门学算法的沉淀,希望能帮助到你~ :partying_face: PS: 在下还有一些其他的文章,欢迎关注~ 1. 基本概念 时间复杂度 空间复杂度 算法的特性和设计原则 2. 数据结构 栈(Stack) ...

    leetcode切割分组-algorithm-java:Java实现的算法

    leetcode切割分组 The algorithms implemented in Java Project Description sort the sort algorithm ds the data structure:stack/list/linked list/queue sort Name Time Complexity Space Complexity Stable ...

    cpp-算法精粹

    Insertion Sort List 归并排序 Merge Two Sorted Arrays Merge Two Sorted Lists Merge k Sorted Lists Sort List 快速排序 Sort Colors Kth Largest Element in an Array 桶排序 First Missing Positive 计数排序 H...

Global site tag (gtag.js) - Google Analytics