Insertion Sort List
2016-07-03 00:37
267 查看
Sort a linked list using insertion sort.
Example
Given
分析:
利用insertion sort的原理,把list A的一个node插入到另一个list B里面,这里关键是list B的尾部的处理。
转载请注明出处:cnblogs.com/beiyeqingteng/
Example
Given
1->3->2->0->null, return
0->1->2->3->null.
分析:
利用insertion sort的原理,把list A的一个node插入到另一个list B里面,这里关键是list B的尾部的处理。
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The first node of linked list. * @return: The head of linked list. * cnblogs.com/beiyeqingteng/ */ public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) return head; ListNode dummyNode = new ListNode(Integer.MIN_VALUE); // a new list while(head != null) { ListNode current = dummyNode; while(head.val > current.val && current.next != null && current.next.val < head.val) { current = current.next; } ListNode next = current.next; current.next = head; head = head.next; current.next.next = next; } return dummyNode.next; } }
转载请注明出处:cnblogs.com/beiyeqingteng/
相关文章推荐
- L1,a private conversation
- 算法复习 - 最小生成树算法 Prim、Kruskal(普里姆算法、 克鲁斯卡尔算法)
- Andorid 方法数超过64K的问题
- Hive基本类型使用-类型转换
- 利用maven编译tachyon源码安装
- 参加了Object-Oriented Analysis and Design Using UML培训
- UVA-1585 Score
- 在R语言中无法设置CRAN镜像问题
- Android中Handler的使用方法及实例(基础回顾)
- [C#6] 2-nameof 运算符
- zero-copy
- ServletConfig与ServletContext对象详解
- Eclipse中mvn compile install 等 报错error in opening zip file
- 记一次阳山长途考试
- linux命令: patch
- yum tips
- 【Android系统】自定制View
- 算法复习 - 广度优先遍历、深度优先遍历(BFS、DFS)
- MLiA SVM(三层境界读后感)
- Android学习第一天笔记