当前位置:网站首页>LeetCode Algorithm 24. Exchange the nodes in the linked list in pairs
LeetCode Algorithm 24. Exchange the nodes in the linked list in pairs
2022-06-26 16:33:00 【Alex_ 12 hours a day 6 days a week】
Topic link :24. Two or two exchange nodes in a linked list
Ideas
Algorithm : iteration
data structure : Linked list
Ideas : In fact, it is a bit similar to list flipping , It's just that every time you flip it, you flip it in pairs .
- First create a virtual head node
- As a virtual header node 、 The next node of the virtual head node 、 The next node of the virtual head node is defined as cur、first、second, Exchange two pointer operations
- cur Keep going , Two steps at a time
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;
}
};
边栏推荐
- Scala 基础 (二):变量和数据类型
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- # 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
- Big talk Domain Driven Design -- presentation layer and others
- 10 tf. data
- In a bad mood, I just write code like this
- [from deleting the database to running] the end of MySQL Foundation (the first step is to run.)
- 对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系
- Learn about common functional interfaces
- 5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?
猜你喜欢

Unlock the value of data fusion! Tencent angel powerfl won the "leading scientific and Technological Achievement Award" at the 2021 digital Expo

5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?

IAR工程适配GD32芯片

11 introduction to CNN

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

Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology

若依如何实现接口限流?

【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃

大话领域驱动设计——表示层及其他

用Attention和微调BERT进行自然语言推断-PyTorch
随机推荐
牛客小白月赛50
Codeforces Round #802 (Div. 2)
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
心情不好,我就这样写代码
proxy
Solution for filtering by special string of microservice
Keepalived 实现 Redis AutoFailover (RedisHA)
What is the process of switching C # read / write files from user mode to kernel mode?
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
C语言 头哥习题答案截图
[从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
day10每日3题(3):数组中的字符串匹配
Notes on key review of software engineering at the end of the term
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
Practice of federal learning in Tencent micro vision advertising
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
R language plotly visualization: Violin graph, multi category variable violin graph, grouped violin graph, split grouped violin graph, two groups of data in each violin graph, each group accounts for
100+ data science interview questions and answers Summary - basic knowledge and data analysis
Data analysis - numpy quick start
5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?