[C语言][LeetCode][24]Swap Nodes in Pairs

ego-planner启动参数深度解析:从配置到实战避障应用 本文深入解析EGO-Planner启动参数的配置与实战应用,重点探讨无人机自主导航中的避障性能优化。通过环境感知、运动约束和算法控制三类参数的详细说明,结合ROS和RViz工具,提供从基础配置到高级调优的完整指南,帮助开发者提升无人机在复杂场景下的规划效率和安全性。 阅读详情

题目

Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

标签

Linked List

难度

简单

分析

题目意思是给定一个单链表,交换两个相邻的节点。我的做法是先找到新链表的头结点,然后像交换两个变量的值的方式,来交换两个相邻的节点,这里需要注意的是,交换的时候,需要保存交换节点的前后节点,这里我用left、right去保存,为了交换之后,前后节点不丢失。

C代码实现

struct ListNode* swapPairs(struct ListNode* head) 
{
    struct ListNode *p, *q, *right, *left;
    struct ListNode * ret;

    if(!head || !head->next)
        return head;

    p = head;
    q = p->next;

    if(p && q)
        ret = q;

    while(p && q)
    {
        right = q->next;
        q->next = p;
        left = p;
        p->next = right;
        p = right;

        if(!right || !right->next)
            break;

        if(right->next)
        {
            q = p->next;
            left->next = q;
        }
    }

    return ret;
}
第七章-分布式搜索引擎-ES:全文查询、分词查询、精确查询、地理坐标查询、组合查询(bool、funtion_score)以及RestApi 111 阅读详情

相关推荐

STM32 无线充电接收端硬件设计:从 LC 谐振到 PWM 控制的 5 大关键电路解析

本文深入解析了基于STM32的无线充电接收端硬件设计,重点探讨了LC谐振倍压整流、INA282电流采样、IR2104S半桥驱动、超级电容保护及STM32最小系统等5大关键电路。针对超级电容的恒功率充电需求,详细介绍了PID控制算法优化及PCB设计实战要点,帮助工程师提升无线充电系统的效率和可靠性。

weixin_31774841的博客 389

leetcode24:Swap Nodes in Pairs

题目:Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You

程序员学编程 的专栏 872

Vercel部署个人博客

先去注册一个账号,建议注册一个Github账号后,使用Github账号来登录Vercel。vercel 部署静态资源网站极其方便简单,并且有可观的访问速度,最主要的是免费部署。此时项目已经成功搭建完毕了,点击图片即可跳转到 vercel 所提供的二级域名访问。然后可以直接拉取我的仓库,所有已经配置好了,可以直接使用,建议fork成自己的!然后修改dns就行,注意vercel提供的域名可能访问不了,需要自己绑定!如果有自己的域名,还可以在 vercel 中进行设置。进入Vercel官网。

醉里博客202271.xyz 2374

leetcode24

LeetCode24 ---------Swap Nodes in Pairs 简单来说就是将链表节点两两呼唤,但是是不重复的。 主要是链表的操作,比较简单,注意头结点就好。 代码: class Solution { private: void mySwap(ListNode *p,ListNode*r) { if (p->next==NULL||r->next == NULL)

上善若水 695

QGIS中高效处理CAD数据的两种实用技巧

本文详细解析了在QGIS中高效处理CAD数据的两种核心方法。针对DWG/DXF文件在QGIS中直接打开失败的问题,介绍了使用QGIS内置数据导入管理器进行标准化转换的流程,并重点推荐了功能更强大的Another DXF Importer插件,以更好地保留文字、块属性等复杂信息。文章对比了两种方法的优劣与适用场景,并提供了坐标系设置、中文乱码等常见问题的避坑指南,帮助用户根据数据需求选择最佳导入策略。

weixin_28363123的博客 122

Brainstorm简单分析近红外数据(fNIRS)(一)

Brainstorm分析fNIRS数据

weixin_43604817的博客 1357

LeetCode 24--两两交换链表中的节点 ( Swap Nodes in Pairs ) ( C语言版 )

题目描述  :   解题思路 : 分为两种情况处理 , 当要交换的节点是链表的前两个节点时 , 当交换的节点非前两个节点时 ; 代码如下 :  /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ stru...

【Dyson】的博客 711

别再混淆了!5分钟搞懂机器人导纳控制与阻抗控制的本质区别(附Simulink对比仿真)

本文深入解析机器人柔顺控制中的导纳控制与阻抗控制的本质区别,通过信号流方向、架构对比和应用场景三个关键视角进行详细阐述。文章还提供Simulink对比仿真案例,帮助读者快速理解两种控制策略的差异及其适用场景,特别适合机器人控制领域的研究者和工程师参考。

weixin_42543046的博客 620

LeetCode 24. Swap Nodes in Pairs(单链表)

这道题只打败了39%的人,呜呜呜,大家都是 O(N) 线性时间,怎么别人就那么优秀呢-  -单链表操作,除了有点烦没其他的。Given a linked list, swap every two adjacent nodes and return its head.Example:Given 1->2->3->4, you should return the list as 2-...

qq_25175067的博客 213

1.用Python写了一个进销存管理的软件~需求分析&界面设计&数据库设计&技术路线选择~

前端也打算用Python来写,查了很多的python库,大家比较推荐的是wxpython和QPython,之前接触过一点点的QT,确实非常强大,但是库比较重量级,自己写的这个东西明显配不上QT,又详细调研了一下wxpython,就是它了!

pepper_lihui的专栏 4325

读书笔记:深度学习入门-基于python的理论与实现(俗称鱼书)

终于从头到尾把鱼书看完啦,真的是非常入门,非常查漏补缺,对新手很友好,对有一定基础的人也非常不错。看完不复习等于没看,所以今天开始复盘,顺便写这篇读书笔记。希望分享给没看过这本书或者看过这本书的同僚们参考,欢迎大家指教。我将从第三章神经网络开始复习,前两章内容书里已经非常基础and清楚了。(会持续更新滴).........

博客而已 1万+

LeetCode-24-两两交换链表中的节点-C语言

使用递归的方法 递归的截至条件为链表小于等于一个节点, 每次仅处理头节点head以及head->next节点,使二者交换。 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ typedef struct L...

SFFS 610

古月居ros2入门21讲里面7.节点:机器人的工作细胞,报错:QSocketNotifier: Can only be used with threads started...如何解决?

🏆本文收录于 《全栈Bug调优(实战版)》 专栏,该专栏专注于分享我在真实项目开发中遇到的各类疑难Bug及其深层成因,并系统提供高效、可复现的解决思路和实操方案。无论你是刚入行的新手开发者,还是拥有多年项目经验的资深工程师,本专栏都将为你提供一条系统化、高质量的问题排查与优化路径,助力你加速成长,攻克技术壁垒,迈向技术价值最大化与职业发展的更高峰🚀!

**My Coding Family** 1827

【LeetCode】24. Swap Nodes in Pairs C语言

LeetCode解题心得,欢迎交流! 第二日 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ //方法1 struct ListNode* swapPairs(struct ListNode* head) {

godlovelong的博客 370

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[leetcode]-24. Swap Nodes in Pairs(C语言)

Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Note: Your algorithm should use only...

shen_zhu的博客 279

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

LeetCode 24. Swap Nodes in Pairs(两两交换链表中的结点)--c语言

24. Swap Nodes in Pairs Given alinked list, swap every two adjacent nodes and return its head. You maynotmodify the values in the list's nodes, only nodes itself may be changed. Example: Given...

d_benhua的博客 285

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode24. 两两交换链表中的节点 Swap Nodes in Pairs(C语言)

题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的额外空间。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 题目解答: 方法1:遍历交换 为了保证逻辑简单,排除特殊情况,插入头结点,另外使用三个指针。 运行时间0...

hang404的博客 512

C语言链表交换相邻节点,LeetCode 24--两两交换链表中的节点 ( Swap Nodes in Pairs ) ( C语言版 )...

题目描述 : 解题思路 : 分为两种情况处理 , 当要交换的节点是链表的前两个节点时 , 当交换的节点非前两个节点时 ;代码如下 :/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* swapPairs(...

weixin_39592240的博客 302

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

水果君の日常 2576

LeetCode //C - 24. Swap Nodes in Pairs

【代码】LeetCode //C - 24. Swap Nodes in Pairs。

Made in Code 532

Leetcode 24. Swap Nodes in Pairs

leetcode 24. Swap Nodes in Pairs /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public cla

独钓寒江雪 434

Leetcode 24 - Swap Nodes in Pairs

题目 https://leetcode.com/problems/swap-nodes-in-pairs/ 题意 给出一个链表。要求两两交换第i个位置和第i+1个位置的节点。(若下标从0开始用,则i是偶数) Example: Given 1->2->3->4, you should return the list as 2->1->4->3. 思路 autho...

weixin_34348174的博客 125

leetcode第24题——swap nodes in pairs 高效解法

问题描述 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list’s nodes, only nodes itself may be changed. Example: Given 1->2->3->4, yo...

apollo_steve的博客 337

LeetCode-24. Swap Nodes in Pairs

24.Swap Nodes in Pairs Medium 1363120FavoriteShare Given alinked list, swap every two adjacent nodes and return its head. You maynotmodify the values in the list's nodes, only nodes itself may...

保持对编程的热情! 189

【LeetCode】24. Swap Nodes in Pairs(C++)

地址:https://leetcode.com/problems/swap-nodes-in-pairs/ 题目: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as

Ethan95的博客 249

LeetCode24. Swap Nodes in Pairs(C++/Python)

Given alinked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Note: Your algorithm should use only...

qq_41562704的博客 308
上一篇: [C语言][LeetCode][203]Remove Linked List Elements
下一篇: cJSON用法总结
Timsley
博客等级 码龄12年 50粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值