当前位置:网站首页>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;
}
};
边栏推荐
- Arduino uno + DS1302 simple time acquisition and serial port printing
- C language reading data
- How to create your own NFT (polygon) on opensea
- 【力扣刷题】单调栈:84. 柱状图中最大的矩形
- 清华“神奇药水”登Nature:逆转干细胞分化,比诺奖成果更进一步,网友:不靠精子卵子就能创造生命了?!...
- redis的二进制数组命令
- Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
- SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
- 【力扣刷题】11.盛最多水的容器//42.接雨水
- Angel 3.2.0 new version released! Figure the computing power is strengthened again
猜你喜欢

stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题

国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知

JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序

NFT transaction principle analysis (2)
Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock

3. Keras version model training

Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统

How to implement interface current limiting?

Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system

Simple use of tensor
随机推荐
Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
[机缘参悟-31]:鬼谷子-抵巇[xī]篇-危机是危险与机会并存
Structure the graduation project of actual combat camp
【力扣刷题】单调栈:84. 柱状图中最大的矩形
The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis
我把它当副业月入3万多,新手月入过万的干货分享!
Stepn novice introduction and advanced
若依打包如何分离jar包和资源文件?
股票开户优惠链接,我如何才能得到?在线开户安全么?
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
网页课程设计大作业——华山旅游网
【蓝桥杯集训100题】scratch辨别质数合数 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第15题
TCP congestion control details | 1 summary
C# 读写文件从用户态切到内核态,到底是个什么流程?
Comprehensive analysis of discord security issues
Redis 迁移(操作流程建议)1
数据分析----numpy快速入门
Mono 的一些实例方法
C语言读取数据
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步