147. Insertion Sort List
Sort a linked list using insertion sort.
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
if(head==null||head.next==null)
return head; ListNode pre=head;
ListNode q=head.next;
ListNode p=head;
while(q!=null)
{ if(q.val<=head.val)
{
pre.next=q.next;
q.next=head;
head=q;
if(pre.next==null)
break;
else q=pre.next;
p=head;
}
else
if(q.val>=p.val&&q.val<p.next.val)
{
pre.next=q.next;
q.next=p.next;
p.next=q;
if(pre.next==null)
break;
else q=pre.next;
p=head;
}
else if(q.val>=p.val&&q.val>p.next.val)
{ p=p.next;
}
else if(q.val==p.next.val&&p.next!=q)//用于有重复元素时的排序
{p=p.next;}
else {
p=head;
pre=pre.next;
q=pre.next;
}
ListNode c=head; } return head;
} }
147. Insertion Sort List的更多相关文章
- 【LeetCode】147. Insertion Sort List 解题报告(Python)
[LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- LeetCode OJ 147. Insertion Sort List
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...
- Java for LeetCode 147 Insertion Sort List
Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...
- 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. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...
- 【leetcode】147. Insertion Sort List
Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...
- 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 sort]147. Insertion Sort List
Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
随机推荐
- (转载)Java基础知识总结
写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java语言代码把思路体现出来. 学习新技 ...
- LA 5059 - Playing With Stones
博弈 SG 由于每个a太大,没有办法递推,但是可以找规律 a为偶数 SG(a)=a/2 a为奇数 SG(a)=SG(a/2) 代码: #include <iostream> #inc ...
- ng-repeat的group
http://blog.csdn.net/violet_day/article/details/17023219 一.obj包含 <!doctype html> <html ng- ...
- 上次遗留下来的XMLUtil的问题
·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...
- 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout
贴出布局代码: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" and ...
- 【转发】linux文件系统变为只读的修复
详细解决方法:http://smartmontools.sourceforge.net/badblockhowto.html 相关问题,更换硬盘:http://blog.chinaunix.net/u ...
- jQuery 中 children() 与 find() 用法的区别
1.children() 与 find() 用法的区别 通过children获取的是该元素的下级元素,而通过find获取的是该元素的下级所有元素.
- hdu 2044
ps:好吧,WA了两次,第一次注意到要用long long了...但是printf那里给忘了...又WA.. 代码:#include "stdio.h"long long dp[5 ...
- 外部表与partition
在建立普通表的时候,如果数据是有分区的,在ADD DATA的时候需要指明分区,比方下面的例子: user表,包含 id bigint,name string,然后按照时间(date)来进行分区,路径存 ...
- Linux中的汇编简介
GNU as汇编语法 GNU汇编语法使用的是AT&T汇编它和Intel汇编的语法主要有以下一些不同: AT&T汇编中的立即操作数前面要加上'$',寄存器操作数名前要加上百分号'%',绝 ...