Leetcode: Insertion Sort List
2013-11-14 04:58
393 查看
Sort a linked list using insertion sort.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode insertionSortList(ListNode head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if (head == null) return head; ListNode res = new ListNode(Integer.MIN_VALUE); ListNode pre = res; ListNode cur = null; ListNode next; while (head != null) { next = head; head = head.next; next.next = null; pre = res; cur = pre.next; while (cur != null && cur.val < next.val) { cur = cur.next; pre = pre.next; } pre.next = next; next.next = cur; } return res.next; } }
相关文章推荐
- Mysql sending data 问题
- 好文章收集
- Core Impact 7.5 破解
- Julia on CPU
- Find the First Common Ancestor
- python环境搭建和开发工具的配置【转】
- 在C语言环境下使用google protobuf
- sqlmap注入常见用法一条龙 【转】
- Java Memory Basic
- Django Meta小节(二) 之ModelForm
- jquery总结
- java Hotspot 内存管理白皮书(中文翻译)
- Cantor展开式
- Ubuntu 下配置 Eclipse + Tomcat7 开发环境
- 在C语言环境下使用google protobuf
- 工作三个月总结2
- hook resize
- 《高可用MySQL——构建健壮的数据中心》读书笔记之二
- VS2110。VC++编译错误"error LNK2005: 已经在 XXX.obj 中定义的问题"
- Find Median With Two heaps