lintcode-easy-Insertion Sort List
2016-02-25 15:38
295 查看
Sort a linked list using insertion sort.
Example
Given
思路也比较直接,insertion sort,要注意的就是操作linkedlist的一些问题
Example
Given
1->3->2->0->null, return
0->1->2->3->null.
思路也比较直接,insertion sort,要注意的就是操作linkedlist的一些问题
/** * 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. */ public ListNode insertionSortList(ListNode head) { // write your code here if(head == null || head.next == null) return head; ListNode fakehead = new ListNode(0); fakehead.next = head; ListNode prev = head; ListNode curr = head.next; while(curr != null){ if(curr.val > prev.val){ prev = curr; curr = curr.next; } else{ ListNode p = fakehead; while(p != prev && p.next.val < curr.val) p = p.next; prev.next = curr.next; curr.next = p.next; p.next = curr; curr = prev.next; } } return fakehead.next; } }
相关文章推荐
- 如何在Android中使用汇编语言
- JavaScript 正则表达式中global模式的特性
- PostgreSQL亚洲活动日将于3/17--19在新加坡举行
- cuda shared memory 静态分配和动态分配
- 浅谈 easyui tabs 的href和content属性
- 数据库应用-后缀树及后缀数组(Suffix-Bäume&Suffix-Arraz)-1
- android 6.0权限管理
- 不读书、不吃苦,你要青春干嘛
- linux mount (挂载命令)详解
- Java基础10 接口的继承与抽象类
- 下拉菜单
- 开始学习bizTalk server了
- 【转载】Memcache升级版:CouchBase的安装配置与使用说明
- Memcached(三)Memcached配置参数初解
- 工作中常用工具
- 实例讲解Android应用开发中Fragment生命周期的控制
- Android学习-四大组件(Service)
- **meaven+springmvc 开发**
- patch bonding: don't use stale speed and duplex information解释
- ContentProvider是如何实现数据共享的。