LeetCode-Insertion Sort List
2015-09-28 02:17
411 查看
记清insertion sort 的方法
然后就是dummy!!!
public class Solution {
public ListNode insertionSortList(ListNode head) {
if ( head == null || head.next == null )
return head;
ListNode temp = head;
ListNode dummy = new ListNode(0);
dummy.next = head;
while ( temp.next != null ){
ListNode cur = temp.next;
if ( temp.val > temp.next.val){
ListNode node = dummy.next;
ListNode pre = dummy;
while ( node.val <= cur.val ){
node = node.next;
pre = pre.next;
}
temp.next = cur.next;
cur.next = node;
pre.next = cur;
}
else
temp = temp.next;
}
return dummy.next;
}
}
然后就是dummy!!!
public class Solution {
public ListNode insertionSortList(ListNode head) {
if ( head == null || head.next == null )
return head;
ListNode temp = head;
ListNode dummy = new ListNode(0);
dummy.next = head;
while ( temp.next != null ){
ListNode cur = temp.next;
if ( temp.val > temp.next.val){
ListNode node = dummy.next;
ListNode pre = dummy;
while ( node.val <= cur.val ){
node = node.next;
pre = pre.next;
}
temp.next = cur.next;
cur.next = node;
pre.next = cur;
}
else
temp = temp.next;
}
return dummy.next;
}
}
相关文章推荐
- Training ------ Building Your First App ------ Building Your First App
- 用Android Studio提交项目到github的几个问题
- json动态创建对象并添加属性,遍历属性
- Xcode7 导出企业级(大企业)安装包ipa
- OpenStack Kilo VirtualBox CentOS7 部署问题汇总及解决办法
- GLib 创建自定义事件源
- JAVA两窗体之间的传值
- 练习:关于数字大小排序,求两数的最小公倍数,字母大小写转化;
- ASP.NET学习笔记(001):HTML
- 10亿随机数取最小的10个
- 一次偶然的随想
- [C++ primer]第一章 开始
- 自定义ca,并使用该ca签署证书
- 练习:大小排序,两数的最小公倍数,字母大小写转换;
- 实现简单多人聊天并@功能
- oc非ARC机制实现单例模式
- DIV+CSS须知
- 正则表达式
- LeetCode-Gas Station
- cookie 读写js操作