Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort.
解题思路:
插入排序,JAVA实现如下:
public ListNode insertionSortList(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode root=new ListNode(Integer.MIN_VALUE);
root.next=head;
head=head.next;
root.next.next=null;
ListNode temp=root,temp2=root;
L1:while(head!=null){
temp=root;
while(head.val>temp.next.val){
temp=temp.next;
if(temp.next==null){
temp.next=head;
head=head.next;
temp.next.next=null;
continue L1;
}
}
temp2=head;
head=head.next;
temp2.next=temp.next;
temp.next=temp2;
}
return root.next;
}
Java for LeetCode 147 Insertion Sort List的更多相关文章
- LeetCode 147. Insertion Sort List 链表插入排序 C++/Java
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- leetcode 147. Insertion Sort List ----- java
Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- [LeetCode] 147. Insertion Sort List 解题思路
Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- Leetcode#147 Insertion Sort List
原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...
- [leetcode] 147. Insertion Sort List (Medium)
原题 别人的思路 非常简洁 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} h ...
- [LeetCode]147. Insertion Sort List链表排序
插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
随机推荐
- Tomcat tomcat-users.xml详解
conf/tomcat-users.xml <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rol ...
- jeecms内容显示条数
1.按照1.2.3.4.5顺序显示 <div class="index-news"> [@cms_channel id='1'] <h2><span& ...
- BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)
今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...
- BZOJ1093 最大半连通子图
Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到 ...
- CODEVS 1258 关路灯
写动归终于能不看题解一次A了!(其实交了两次,一次80一次A) 我练功发自真心! 题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每天早晨他必须关掉他所在村庄的街灯.所有的街灯都被 ...
- POJ1703Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37722 Accepted: ...
- ASP.NET MVC 过滤器详解
http://www.fwqtg.net/asp-net-mvc-%E8%BF%87%E6%BB%A4%E5%99%A8%E8%AF%A6%E8%A7%A3.html 我经历了过滤器的苦难,我想到了还 ...
- Spring学习8- SSH需要的jar包
struts2 commons-logging-1.0.4.jar 主要用于日志处理 freemarker-2.3.8.jar 模板相关操作需要包 ognl-2.6.11.jar ognl表达示所需包 ...
- angularJS学习1
<!DOCTYPE html><html ng-app><head> <script src="http://code.angularjs.org/ ...
- php中图片文件的导入,上传与下载
---------------------------------------------图片的导入-------------------------------------------------- ...