leetcode:21. Merge Two Sorted Lists

FC7300 SPI MCAL配置引导 Example - QMI8A01z外设:Example - QMI8A01z外设: 阅读详情

Mgj又不会了,

比较直接的方法是,先拼接,后排序:

 

写的又复杂又乱,,而且没写出来,赶紧学大佬怎么写的吧,,,

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/*
merge it first ,and then sort it
*/


class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* pcur=  (l1->val <= l2->val ? l1 : l2);
        ListNode* head=pcur;
        ListNode* pcomp=(l1->val > l2->val ? l1 : l2);
        ListNode*  pcur_next=NULL;
         ListNode*  pcomp_next=NULL;
        
        while(pcur->next != NULL && pcomp->next != NULL)
        {
            
       
     if(pcur->val < pcomp->val && (pcur->next)->val > pcomp->val )  
     {
         pcur_next=pcur->next;//save next node
         pcomp_next=pcomp->next;
         pcur->next=pcomp;
         pcomp->next=pcur_next;
         pcur=pcur_next;//move pcur
         pcomp=pcomp_next;
         
     }
          }
        return head;
    }
};

 

菜,

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        
        //merge first
         ListNode* pcur= l1;
        while(pcur->next != NULL)
        {
            printf("%d ",pcur->val);
            pcur=pcur->next;
            
        }
        pcur->next=l2;
        
        //sort it
        pcur= l1;
        int temp=0;
        // while(pcur->next != NULL)
        // {
        //     if()
        // }
        
        
        
        return l1;
    }
};

for for 冒泡,,,

 

 

大佬:

mgj真清晰。。。

 

class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode dummy(INT_MIN);
        ListNode *tail = &dummy;
        
        while (l1 && l2) {
            if (l1->val < l2->val) {
                tail->next = l1;
                l1 = l1->next;
            } else {
                tail->next = l2;
                l2 = l2->next;
            }
            tail = tail->next;
        }

        tail->next = l1 ? l1 : l2;
        return dummy.next;
    }
};

 

 


class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        
         ListNode head(INT_MIN);//create a header?
        ListNode *tail = &head;
        
        while(l1!= NULL && l2!= NULL)
        {
            if(l1->val < l2->val)
            {
                tail->next=l1;
                l1=l1->next;
            }else{
                tail->next=l2;
                l2=l2->next;
            }
            tail= tail->next;
        }
        tail->next = l1 ? l1:l2;
        return head.next;
        
    }
};

 

QT C++ AI棉花智能变量灌溉工上位机(信创完整版) 北斗履带灌溉机器人/田间分区电磁阀基站 → 多层土壤温湿度/盐分EC传感器实时采集墒情 → 双目AI视觉扫描棉田长势、病苗分布 → AI融合**生育期+土壤墒情+盐分+棉株长势**计算变量灌溉定额、水肥配比、灌溉时长 → 分区电磁阀/变频水泵变量供水,病弱苗地块减量、干旱地块增量;墒情、盐分、长势多因子闭环修正灌水量;多层土壤含水率(0–10/20–40/40–80cm)、土壤EC盐分、土壤温度、空气温湿度、蒸发量、北斗地块坐标、管道水压、水肥罐液位、分区流量计、水泵电机负载、滴灌带压力反馈。 阅读详情

相关推荐

Kubernetes集群环境搭建与初始化

Kubernetes是Google开源的一个容器编排引擎,它支持自动化部署、大规模可伸缩、应用容器化管理。在生产环境中部署一个应用程序时,通常要部署该应用的多个实例以便对应用请求进行负载均衡。在Kubernetes中,我们可以创建多个容器,每个容器里面运行一个应用实例,然后通过内置的负载均衡策略,实现对这一组应用实例的管理、发现、访问,而这些细节都不需要运维人员去进行复杂的手工配置和处理。

m0_75254642的博客 1534

LeetCode 21:Merge Two Sorted Lists

LeetCode 21: Merge Two Sorted Lists   Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目分析:对两个

哈哈的个人专栏 4761

PostgreSQL高可用之主备复制

多个PostgreSQL可协同工作,如果主库发生宕机备库将迅速进行接管并对外提供服务以实现高可用。本文将从主备复制、主备切换、故障转移等角度进行实际测试演练。第一章主备复制是基础,通过主备复制可以得到一个或多个相同的实例。在主备复制的基础上通过第二章主备切换介绍了如何手工将主库与备库互换角色。在第三章中,采用了Keepalived进行实时监测,在主库宕机后,使用主备切换中提到的命令使备库升级为主库并对外提供服务,以达到PostgreSQL高可用的目标。

asdasdsaff的博客 1350

leetcode 21: Merge Two Sorted Lists

leetcode 21: Merge Two Sorted Lists

rongwenxiao的专栏 380

基于深度强化学习技术优化无线网络中的资源分配(一)DQN部分代码

这是一个GitHub上的开源项目,链接:https://github.com/alexis-anzaldo/Deep-Reinforcement-Learning-for-resource-allocation-in-wireless-networks该项目旨在通过深度强化学习DQL技术优化无线网络中的资源分配,特别关注小型蜂窝和超密集无线蜂窝网络中的功率控制问题,主要目标是提高DRL模型的学习效率和网络吞吐量。

m0_46292040的博客 1543

TongWeb微服务部署实战:Nacos注册与网关路由的疑难解析

本文分享了将Spring Cloud微服务迁移至TongWeb应用服务器时,解决Nacos服务注册失败与Spring Cloud Gateway路由失效的实战经验。针对Nacos注册问题,通过实现ApplicationRunner接口并利用JMX获取容器端口,实现了手动注册;针对网关路由问题,建议将基于WebFlux的Gateway模块改为JAR包独立部署,以保障其原生运行环境。文章为在TongWeb上进行微服务部署提供了关键的问题排查思路与混合架构部署的最佳实践。

weixin_29209801的博客 247

【LeetCode】LeetCode——第21题:Merge Two Sorted Lists

【LeetCode】LeetCode——第21题:Merge Two Sorted Lists

hujingshuang 1861

【干货攻略】Oracle报错篇 ORA-01378 磁盘扇区大小不兼容问题

本期将为大家分享“ORA-01378 磁盘扇区大小不兼容”报错的问题解决方案。

PWY13148的专栏 1442

LeetCode.21: Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1-&gt...

qq_32350719的博客 249

利用ArcGIS的模型构建器实现批量投影栅格和批量克里金插值

博主要批量投影几百个栅格,但是ArcGIS中的投影栅格的批处理工具需要手动一条条设置参数,还是麻烦,因此学习了使用模型构建器(ModelBuilder)来批量投影栅格,可以导入需投影栅格的文件夹,然后迭代投影,并按栅格原名逐个输出到指定目录。 1.在工具箱里构建模型 在arctool box里右键点击空白处,选择【添加工具箱】,点击ArcGIS默认工作目录的工具箱进行添加,然后Toolbox就会被添加到ArcToolbox页面,右键点击Toolbox,选择“新建”-“模型” 2.编写模型 (1).

qq_36958801的博客 1万+

从零开始理解Linux中断架构(1)-前言

前段时间在转行手撸WIFI路由器,搞wifi路由器需要理解网络驱动程序,以太网卡驱动程序,无线WIFI驱动程序,而网卡驱动的关键路径就在中断程序中,需要了解NIC设备驱动程序如何收发数据,为了彻底的知道数据包是如何二层传递上来的,又需要了解一点Linux中断系统。第一次看到这张表是无感的,大概能够看懂的就是那个中断次数,因为不停的cat,这个数值会一直不停的增加。第一列是逻辑中断号,第二三列是中断次数,第四列是中断控制器的名字,第五列是硬件中断号,第六列 中断类型,第七列是 中断源关联设备。

maxleng的专栏 883

Leetcode21:Merge Two Sorted Lists

题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: ...

leihanwen的博客 223

[Python图像处理] 二.OpenCV+Numpy库读取与修改像素

该系列文章是讲解Python OpenCV图像处理知识,前期主要讲解图像入门、OpenCV基础用法,中期讲解图像处理的各种算法,包括图像锐化算子、图像增强技术、图像分割等,后期结合深度学习研究图像识别、图像分类应用。希望文章对您有所帮助,如果有不足之处,还请海涵~ 同时推荐作者的C++图像系列知识: [数字图像处理] 一.MFC详解显示BMP格式图片 [数字图像处理] 二.MFC单文档分...

杨秀璋的专栏 3万+

LeetCode 21:Merge Two Sorted Lists

Problem: Merge Two Sorted Lists   Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. /** * Defin

chenxian90的专栏 300

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Leetcode1-100: 21. Merge Two Sorted Lists

Leetcode1-100: 21. Merge Two Sorted Lists问题描述解题思路代码实现复杂度分析分析 问题描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Out

Y123iwantit的博客 265

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Leetcode21:Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1-&gt...

qq_34314736的博客 147

Leetcode21: Merge Two Sorted Lists

还是和链表有关的问题,和第二题比较像,代码如下 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object):

bestallen的博客 520

LeetCode:21. Merge Two Sorted Lists

一、问题描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Ou...

ixiaowei1993的博客 168

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'21: Merge Two Sorted Listshttps://oj.leetcode.com/problems/merge-two-sorted-lists/Merge two sorted linked lists and return it as a new ...

aaron7909的博客 113

leetcode: 21. Merge Two Sorted Lists

Difficulty Easy Description Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1-&gt;2-&gt;...

lahhass的博客 190

LeetCode: 21. Merge Two Sorted Lists / 23. Merge k Sorted Lists

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4...

La745739773的博客 226

Leetcode: 21. Merge Two Sorted Lists

Description Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路 合并两个有序链表,这个没啥好说的吧 代码 /** * Definiti...

anrui0096的博客 128

leetcode[21]:Merge Two Sorted Lists

Merge Two Sorted ListsMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Definition for singly-linked li

hahubaba的博客 234

LeetCode:21. Merge Two Sorted Lists

LeetCode:21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1-&g...

Wisimer 116

leetcode(21): Merge Two Sorted Lists

题目 翻译: 合并2个已经排序的链表,并且返回一个新的链表。这个新的链表应该由前面提到的2个链表的节点所组成。 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. E...

博客 322

LeetCode 21: Merge Two Sorted Lists

Merge Two Sorted ListsMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.解题思路比较简单,代码如下:/** * Definition for si

一只码畜 235
上一篇: ffos:Ffos下的系统升级方法
下一篇: leetcode:118. Pascal's Triangle
nedwons
博客等级 码龄9年 311粉丝 572原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值