当前位置:网站首页>LeetCode Algorithm 24. 两两交换链表中的节点
LeetCode Algorithm 24. 两两交换链表中的节点
2022-06-26 16:18:00 【Alex_996】
题目链接:24. 两两交换链表中的节点
Ideas
算法:迭代
数据结构:链表
思路:其实有点类似于链表翻转,只不过每次翻转是两两翻转的。
- 首先创建一个虚拟头节点
- 以虚拟头节点、虚拟头节点的下一个节点、虚拟头节点的下下个节点分别定义为cur、first、second,进行指针操作两两交换
- cur继续往前走,一次走两步
Code
C++
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode *cur = dummyHead;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode *first = cur->next, *second = cur->next->next;
cur->next = second;
first->next = second->next;
second->next = first;
cur = first;
}
return dummyHead->next;
}
};
边栏推荐
- Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
- IAR engineering adapts gd32 chip
- 心情不好,我就这样写代码
- 精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
- mha 切换(操作流程建议)
- # 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
- TCP拥塞控制详解 | 1. 概述
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- 【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
- C# 读写文件从用户态切到内核态,到底是个什么流程?
猜你喜欢

用Attention和微调BERT进行自然语言推断-PyTorch

油田勘探问题

长安链交易防重之布谷鸟过滤器

Développer un opérateur basé sur kubebuilder (démarrer)

基於Kubebuilder開發Operator(入門使用)

I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!

架构实战营毕业设计

Learn about common functional interfaces

Cloud platform monitoring system based on stm32+ Huawei cloud IOT design

100+ data science interview questions and answers Summary - basic knowledge and data analysis
随机推荐
Solution for filtering by special string of microservice
清华“神奇药水”登Nature:逆转干细胞分化,比诺奖成果更进一步,网友:不靠精子卵子就能创造生命了?!...
H5 close the current page, including wechat browser (with source code)
Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction
How to implement interface current limiting?
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
Scala 基础 (二):变量和数据类型
How to create your own NFT (polygon) on opensea
Redis 迁移(操作流程建议)
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
Pybullet robot simulation environment construction 5 Robot pose visualization
Data analysis - numpy quick start
基于Kubebuilder开发Operator(入门使用)
架构实战营毕业设计
数据分析----numpy快速入门
1-12Vmware新增SSH功能
Simple use of tensor
长安链交易防重之布谷鸟过滤器
100+ data science interview questions and answers Summary - basic knowledge and data analysis