Sort a linked list using insertion sort.

问题:实现单向链表的插入排序。

这是比较常规的一个算法题目。 从左往右扫列表,每次将指针的下一个元素插入前面已排好序的对应位置中。

需要注意的一定是,列表只能定位下一个元素,不能定位前一个元素,所有,每次插入位置的适合,都是对左右指针的下一个元素进行操作。

     void insertSort(ListNode* p1, ListNode* p2){
ListNode* next2 = p2->next;
p2->next = p2->next->next;
next2->next = p1->next;
p1->next = next2;
} ListNode* insertionSortList(ListNode* head) { if (head == NULL){
return head;
} ListNode* r = head; while(r->next != NULL){
ListNode* l = new ListNode();
l->next = head;
while(l != r){
if (l->next->val > r->next->val){ if (l->next == head) {
head = r->next;
} insertSort(l, r);
break;
}
l = l->next;
} if (r->next != NULL && r->next->val >= r->val) {
r = r->next;
}
} return head;
}
 
 

[LeetCode] 147. Insertion Sort List 解题思路的更多相关文章

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

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

  2. Java for LeetCode 147 Insertion Sort List

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

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

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

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

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

  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 (Medium)

    原题 别人的思路 非常简洁 function ListNode(val) { this.val = val; this.next = null; } /** * @param {ListNode} h ...

  7. Leetcode#147 Insertion Sort List

    原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...

  8. [LeetCode]147. Insertion Sort List链表排序

    插入排序的基本思想 把排好的放在一个新的变量中,每次拿出新的,排进去 这个新的变量要有超前节点,因为第一个节点可能会有变动 public ListNode insertionSortList(List ...

  9. [leetcode sort]147. Insertion Sort List

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

随机推荐

  1. Task的运行过程分析

    Task的运行过程分析 Task的运行通过Worker启动时生成的Executor实例进行, caseRegisteredExecutor(sparkProperties)=> logInfo( ...

  2. 招一位安防软件project师,嵌入式开发project师

    岗位职责 1.负责海思平台IPC产品应用层软件设计及维护 2.私有平台协议对接及为第三方提供技术支持. 任职资格: 1.较强的学习.领悟能力,能够高速熟悉公司现有代码. 2.熟练掌握C.C++开发语言 ...

  3. C++11 tuple

    tuple元组定义了一个有固定数目元素的容器,其中的每个元素类型都可以不相同,这与其他容器有着本质的区别.是对pair的泛化. 首先来介绍元组的创建和元组元素的访问.通过make_tuple()创建元 ...

  4. JAVA - Comparable接口 与 Comparator接口

      Similarities:Both are custom ways to compare two objects.Both return an int describing the relatio ...

  5. 配置NFS服务器

    一.配置NFS服务器 1.安装软件包 [root@wjb10000 ~]# yum -y install nfs-utils.x86_64 2.修改配置文件[root@wjb10000 ~]# vim ...

  6. JPush 极光推送 消息推送 实例

    简介 官网:https://www.jpush.cn/ 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度 ...

  7. ShareSDK 社会化分享 集成步骤

    第一步 :获取ShareSDK 官网:http://www.mob.com 完整的集成文档:http://wiki.mob.com/android-sharesdk%E5%AE%8C%E6%95%B4 ...

  8. web03--session

    1.创建session1.jsp <body> <form action="session2.jsp" method="post"> & ...

  9. webform 复杂点的服务器控件

    1  , dropdownlist:  下拉框 属性items  列表集合,  里面的每一个元素是一个 listitem . 联动的时候注意要 设置属性 .Autopostback 为ture: 注注 ...

  10. bootstrap初探

    bootstrap资源 http://getbootstrap.com http://github.com/twbs http://www.bootcss.com bootstrap栅格系统 容器:流 ...