insertion-sort-list

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

Sort a linked list using insertion sort.


/**

 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        if(!head || !head->next){
            return head;
        }
        
        ListNode *qpre = head;
        ListNode *q = head->next;
        while (q){
            ListNode *p = head;
            ListNode *ppre = NULL;
            ListNode *cur = q;
            while(p!=q && p->val < q->val){
                ppre = p;
                p = p->next;
            }
            if (p!=q){
                cur = q->next;
                qpre->next = q->next;


                if(ppre){
                    q->next = ppre->next;
                    ppre->next = q;
                }else{
                    q->next = p;
                    head = q;
                }
                q = cur;
            }else{
                qpre = q;
                q = q->next;
            }
            
        }
        return head;
    }
};
地平线天工开物AI开发平台实战:从量化训练到模型部署的完整指南 本文提供了地平线天工开物(OpenExplorer)AI开发平台的实战指南,详细解析了从量化训练到模型部署的完整流程。文章重点对比了量化感知训练(QAT)与训练后量化(PTQ)两条核心路径的优劣与适用场景,并分享了图像分类模型PTQ部署的实战经验与常见问题调试技巧,旨在帮助开发者高效地将AI模型部署到地平线AI芯片上。 阅读详情

相关推荐

SPD5 集线器协议内容解析

​ 若不同SPD5 Hub后面的本地侧设备同时发送请求,则是22和23、24的混合过程,先通过LID裁决,再通过HID裁决,这里的LID是本地设备的,若LID一致,则看HID,由于SPD5 Hub的本地侧设备HID均为111,因此实际上是比较SPD5 Hub的HID,这个过程在前文提到,SPD5 Hub会转换HID。​ 若MR27[4]=1且MR27[3:0]=1,如果MR51[3:0]=1时,发送IBI,并且MR48[7]置1,将GETSTATUS CCC的待处理中断位[3:0]更新为0001。

weixin_50591344的博客 3835

Leetcode147. 对链表进行插入排序

下面是插入排序算法的一个图形示例。部分排序的列表(黑色)最初只包含列表中的第一个元素。每次迭代时,从输入数据中删除一个元素(红色),并就地插入已排序的列表中。对链表进行排序,并返回。

neverSaynever_的博客 240

基于Gurobi的综合能源系统运行优化:鲁棒两阶段优化方法

本文提出了一种基于Gurobi的综合能源系统鲁棒两阶段优化方法,针对包含换电站、光伏发电和电池储能的系统运行问题。研究采用多面体不确定性集合描述光伏出力随机性,构建了考虑电池衰减成本和弃光惩罚的两阶段优化模型:第一阶段制定日前调度计划,第二阶段处理实时运行中的不确定性。通过列与约束生成算法求解模型,并使用Python和Gurobi实现。仿真验证了该方法能有效平衡系统经济性与鲁棒性,为含高比例可再生能源的综合能源系统运行提供了实用解决方案。

huanghm88的专栏 224

#147 Insertion Sort List

Description Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list’s head. The steps of the insertion sort algorithm: Insertion sort iterates, consuming one input element each repetition and growing a sorted

YY_Tina的博客 298

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-pl

bool的博客 225

Leetcode Insertion Sort List 解题报告

http://oj.leetcode.com/problems/insertion-sort-list/ Sort a linked list using insertion sort. 基本分析:用插入排序对一个链表进行排序。 一开始写的时候想套用数组的插入排序,结果发现无法对末节点加上NULL。 对链表进行插入排序的正确方法是:新建一个头节点,遍历原来的链表,在新链表中找到适合的位

算法的天空 6625

LeetCode: Insertion Sort List

LeetCode: Insertion Sort List LeetCode: Insertion Sort List Sort a linked list using insertion sort. 地址:https://oj.leetcode.com/problems/insertion-sort-list/ 算法:按题目的意思,采用...

weixin_34185512的博客 184

leetcode - Insertion Sort List

题目:Insertion Sort List Sort a linked list using insertion sort. 题目比较简单,就是对链表进行插入排序 个人思路: 1,用插入排序的方法来解就行,注意链表的处理,可以给原链表加个头结点来使得处理统一(我的代码没有加,懒得改了) 代码: 1 #include <stddef.h> 2 3 ...

weixin_30257433的博客 82

[leetcode] 147. Insertion Sort List

Description 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 ...

Learning from the mistakes 367

[Leetcode Week16]Insertion Sort List

Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ Description Sort a linked list using insertion sort. Solution class Solution { private: void ...

weixin_30349597的博客 143

LeetCode Insertion Sort List

原题链接在这里:https://leetcode.com/problems/insertion-sort-list/ 思路:设置cur = head,判断cur.val 和 cur.next.val 大小关系,若是后者小, 就从头往后扫值,一直到有一点的val大于cur.next.val, 然后把cur.next这个点加在那里。 Note: 1. 有可能会更改head,所以加一个dummy h

Dylan_Java_NYC的博客 486

147. Insertion Sort List

Description: Sort a linked list using insertion sort. Analysis: 插入排序,时间复杂度O(N^2) 创建一个dummy node,方便从head开始遍历。 可以结合题目148一起看。 两种解法: 解法一: 断开链表,分成两个链表,链表1是有序链表,链表2是待排序链表,依次将链表2中的元素插入到链表1的有序链表中; 解法二: 不断开链表,依次移动结点,移动结点后,依然保持链表不断开。 Solution1: class Solution { publ

willinux的专栏 392

【LeetCode】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) {

跳出温水的青蛙 1950

【leetcode】Insertion Sort List

Sort a linked list using insertion sort. 建立一个新头结点,然后依次将head链表中的结点有序的插入到新链表中 /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode

唐辰砂的博客 497

[LeetCode]Insertion Sort List

Sort a linked list using insertion sort. Analysis The general idea is insert the current element A[i] into the proper position from A[0]...A[i-1], and A[0]...A[i-1] is already sorted. In this p

coder 进阶的专栏 2389

147 Insertion Sort List

题目链接:https://leetcode.com/problems/insertion-sort-list/题目:Sort a linked list using insertion sort.解题思路: 1、插入排序的通用算法 2、链表的插入排序和数组的插入排序存在差异: 链表:当前要插入的元素,对已排好序的链表从前往后比较 数组:当前要插入的元素,对已排好序的数组从后往前比较(需要往后挪元

再见了,过去的名字,新的奋斗开始啦! 556
上一篇: C++中容器总结
下一篇: 递归函数yu尾递归
macans
博客等级 码龄11年 72粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值