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的更多相关文章

  1. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  2. [LeetCode] 147. Insertion Sort List 链表插入排序

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  3. LeetCode OJ 147. Insertion Sort List

    Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链 ...

  4. Java for LeetCode 147 Insertion Sort List

    Sort a linked list using insertion sort. 解题思路: 插入排序,JAVA实现如下: public ListNode insertionSortList(List ...

  5. leetcode 147. Insertion Sort List ----- java

    Sort a linked list using insertion sort. 插入排序. /** * Definition for singly-linked list. * public cla ...

  6. [LeetCode] 147. Insertion Sort List 解题思路

    Sort a linked list using insertion sort. 问题:实现单向链表的插入排序. 这是比较常规的一个算法题目. 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的 ...

  7. 【leetcode】147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...

  8. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  9. [leetcode sort]147. Insertion Sort List

    Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...

随机推荐

  1. uboot启动 及命令分析(3)

    u-boot命令 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd_tbl_s { char     *name;   /* ...

  2. 纯JS省市区三级联动

    代码下载

  3. addChildViewController

    http://www.cnblogs.com/zengyou/p/3386605.html //在parent view controller 中添加 child view controller Fi ...

  4. Android 动画特效

    一.渐变动画 AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f); // fromAlpha , toAlpha aa.setDuration(200 ...

  5. dataTable/dataSet转换成Json格式

    using System.Text;using System.Collections.Generic; 1)dataTable转Json(表格有名称:dt.TableName) public stat ...

  6. 使用generator自动生成mybatis model、mapper.xml、mapper等(转)

    原文链接:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件 ...

  7. idea常用快捷键大全(转)

    IntelliJ Idea 常用快捷键列表   文章来自:http://lavasoft.blog.51cto.com/62575/97730/   Alt+回车 导入包,自动修正Ctrl+N   查 ...

  8. Android 自带图标库 android.R.drawable

    在xml文件中调用. android:title="@string/secure_connect"android:orderInCategory="100"an ...

  9. Java基础毕向东day03

    Java基础毕向东day03 1.变量 2.条件结构 3.循环结构,for while,和几种特殊的情况. 4.函数重载

  10. fwrite错误

    使用fwrite出错 f:\dd\vctools\crt_bld\self_x86\crt\srt\write.cline:69expression:_osfile(fh)&FOPEN 使用w ...