1. Insertion Sort List

Sort a linked list using insertion sort.



A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.

With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

  1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
  3. It repeats until no input elements remain.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:

Input: -1->5->3->4->0
Output: -1->0->3->4->5

插入排序

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(head == NULL || head->next == NULL)return head;
ListNode *hh = new ListNode(INT_MIN, head);
ListNode *q = head->next, *p = head;
while(q){
ListNode *tmp = hh->next, *pre = hh;
while(tmp != q && tmp->val < q->val){
pre = tmp;
tmp = tmp->next;
}
if(tmp != q){
p->next = q->next;
q->next = tmp;
pre->next = q;
q = p->next;
}else{
p = p->next;
q = q->next;
}
}
return hh->next;
}
};

【刷题-LeetCode】147 Insertion Sort List的更多相关文章

  1. 【leetcode刷题笔记】Insertion Sort List

    Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻 ...

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

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

  3. Java for LeetCode 147 Insertion Sort List

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

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

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

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

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

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

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

  7. Leetcode#147 Insertion Sort List

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

  8. [leetcode] 147. Insertion Sort List (Medium)

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

  9. 【刷题-LeetCode】148 Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4 ...

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

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

随机推荐

  1. CF1092B Teams Forming 题解

    Content 有 \(n\) 个学生,每个学生有一个能力值 \(a_i\).现在想把学生两两分成一组,但是不能让每个组里面的学生能力值不相同,因此可以通过刷题来提升自己的能力值,每次解出一道题,能力 ...

  2. js Uncaught TypeError: undefined is not a function

    如下代码: var columns={}; var column={}: column.name='张三'; columns.push(column); 会出现Uncaught TypeError: ...

  3. js(JQuery)引入select2

    官方项目地址:https://select2.org/ 引入css和js <link href="https://cdnjs.cloudflare.com/ajax/libs/sele ...

  4. 利用免费二维码API自动生成网址图片二维码

    调用第三方接口生成二维码 官方地址:http://goqr.me/api/ 示例 https://api.qrserver.com/v1/create-qr-code/?size=180x180&am ...

  5. Log4j未平,Logback 又起!再爆漏洞?

    前段时间 Log4j接连爆漏洞的事儿相比把大家都折腾的不轻,很多开发都被连夜叫起来修复漏洞.这几天终于平复一些了. 可是,昨晚,忽然看到技术群和朋友圈,有人开始聊Logback 又爆漏洞了. 这是什么 ...

  6. ubuntu 16.04 server安装Bittorrent Transmission

    访问web服务 使用http://192.168.1.8:9091 这样的方式管理下载. http://192.168.1.8:9091/transmission/web/ 操作服务 sudo ser ...

  7. c++指针函数和函数指针概述

    欢迎指正 代码写的不够规范: 目的是缩短文章篇幅,实际中请注意 阅读完本文, 你一定能判断和写出:指针函数和函数指针. 0.结论 A.指针函数: 函数的返回值是指针类型 B.函数指针: 函数名是一个指 ...

  8. 教学日志:javaSE-数组

    一.一维数组 import java.util.Scanner; /* 数组:存储一组相同数据类型的有序集合. 特点: 1.数组中的元素必须是同一种数据类型,可以是基本数据类型,也可以是引用数据类型 ...

  9. API 网关功能

    反向代理和路由 - 大多数项目采用网关的解决方案的最主要的原因.给出了访问后端 API 的所有客户端的单一入口,并隐藏内部服务部署的细节. 负载均衡 - 网关可以将单个传入的请求路由到多个后端目的地. ...

  10. Linq和Lambda 性能对比

    Linq和Lambda 性能对比 1.Where() 使用LINQ创建一个简单的where查询 var query = from person in PersonCollection where pe ...