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. CF1491A K-th Largest Value 题解

    Content 你有一个长度为 \(n\),并且仅包含 \(0/1\) 的数组 \(a\).现在对这个序列做以下两种操作之一共 \(q\) 次: \(1\) \(x\):将 \(a_x\) 修改为 \ ...

  2. CF1445B Elimination 题解

    Content 一个比赛分两场进行,其中: 第一场的第一百名成绩为 \(a\),且第一场的前一百名在第二场中都至少得到了 \(b\) 分. 第二场的第一百名成绩为 \(c\),且第二场的前一百名在第一 ...

  3. dump Java 程序和服务器相关信息

    #!/bin/bash jps -lm read -p "enter java pid: " pid port=$(netstat -ntlp | grep $pid | awk ...

  4. SQL:大表多表更新的两种方法

    #标记不参与计算的明细(跨平台的或is_end=2)#跨平台订单:暂不处理 说明:大表即order_list_wx,几十万,需要根据小表(order_list_zfb ,几万)来做更新,查出两个平台都 ...

  5. 10分钟快速上车短视频风口:基于uniapp框架创建自己的仿抖音短视APP

    在今年也就是第48次发布的<中国互联网络发展状况统计报告>有这样一个数据,21年的上半年以来,我国我国网民规模达10.11亿,其中短视频用户达8.88亿.碎片化的生活场景下,短视频成为人们 ...

  6. c++11之 algorithm 算法库新增 minmax_element同时计算最大值和最小值

    0.时刻提醒自己 Note: vector的释放 1. minmax_element 功能 寻找范围 [first, last) 中最小和最大的元素. 2. 头文件 #include <algo ...

  7. 【剑指Offer】构建乘积数组 解题报告(Python)

    [剑指Offer]构建乘积数组 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目 ...

  8. 最大流问题的Ford-Fulkerson模板

    详细讲解:http://blog.csdn.net/smartxxyx/article/details/9293665 下面贴上我的第一道最大流的题: hdu3549 1 #include<st ...

  9. 阿里云视觉智能开放平台的人脸1:N搜索的开源替代-Java版(文末赋开源地址)

    ​ 一.人脸检测相关概念 人脸检测(Face Detection)是检测出图像中人脸所在位置的一项技术,是人脸智能分析应用的核心组成部分,也是最基础的部分.人脸检测方法现在多种多样,常用的技术或工具大 ...

  10. 替代瑞昱RTD2166|pin对pin替代RTD2166|CS5202芯片

    替代瑞昱RTD2166,pin对pin替代RTD2166,外围器件少,设计版框尺寸小,整套方案成本BOM更低. 一. CS5202功能概述  CS5202是一款DP端口到VGA转换器,它结合了DP输入 ...