当前位置:网站首页>LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
2022-06-11 16:50:00 【Always--Learning】
Title Description

Their thinking
- Define a pre node , Its next Domain points to head.
- Define a temporary pointer to pre.
- As long as the current node and the next node of the current node are not empty , Then enter the cycle .
- After entering the cycle , First save the next node of the current pointer .
- pre Point to next.
- cur.next = next.next;
- next.next = cur;
- pre = cur;
- cur = cur.next;
AC Code
var swapPairs = function(head) {
// The core of the node in the pairwise exchange linked list is to change through three pointers
if (!head) return null;
let pre = new ListNode('-');
pre.next = head;
let temp = pre;
let cur = head;
while (cur && cur.next) {
let next = cur.next
pre.next = next;
cur.next = next.next;
next.next = cur;
pre = cur;
cur = cur.next;
}
return temp.next;
};
reflection
The title of the linked list class seems simple , It's not , There are a lot of linked list problems that burn your brain , Especially if you just don't want to paint , Many ideas can only be drawn by hand , I soon knew how to do it , But it's hard to figure it out just in your head , So be sure to draw pictures . There are many similar topics , For example, reverse linked list ,K A group of flipped lists are common questions in the interview .
Icon

What is more difficult to think about is that in the process of moving pre and cur Who is it pointing to , Once we understand the direction of both of them, we can solve this problem , As long as the topic can be clarified , Similar problems such as list turnover will be solved , It is suggested to practice this kind of topic more , Almost all such topics involve pre,cur and next, there next It is often necessary to define... In a loop .
Reference link
边栏推荐
- 2022g1 industrial boiler stoker test questions and simulation test
- Center detection of circular marking points for opencv camera calibration
- If you want to learn ArrayList well, it is enough to read this article
- leetcode684. Redundant connection (medium)
- Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte
- LeetCode——42. 接雨水(双指针)
- Message queue push / pull mode Learning & ActiveMQ and JMS learning
- 从0到1了解Prometheus
- Redis - learn five types of NoSQL
- Connection and difference of network streaming media protocol (RTP RTCP RTMP HLS)
猜你喜欢

RSP:遥感预训练的实证研究

数据库全量SQL分析与审计系统性能优化之旅

GemBox. Bundle 43.0 Crack

VLAN partition and routing between VLANs

2022 national question bank and mock examination for safety officer-b certificate

2022年安全员-B证国家题库及模拟考试

Redis --- 学习 NoSQL 五大类型

Go语言之Go 快速入门篇(一):第一个 Go 程序

^31 prototype interview questions

Go quick start of go language (I): the first go program
随机推荐
2022g1 industrial boiler stoker test questions and simulation test
Global and Chinese molten carbonate fuel cell industry outlook and market panoramic Research Report 2022-2028
Wechat applet timestamp conversion time format + time subtraction
微服务连接云端Sentinel 控制台失败及连接成功后出现链路空白问题(已解决)
消息队列-推/拉模式学习 & ActiveMQ及JMS学习
ASP. Net education OA system source code education industry OA system source code with document
药物评价指标
2022 simulated examination question bank and simulated examination for crane driver (limited to bridge crane)
The micro service failed to connect to the cloud sentinel console and the link blank problem occurred after the connection was successful (resolved)
JVM 的组成
Exception handling and exception usage in golang
Oracle 分析函数 over 和MySQL 实现类似效果写法
Analysis report on sales status and supply and demand prospects of phosphoric acid fuel cell industry in the world and China 2022-2028 Edition
一套ThinkPHP微信小程序商城源码带后台管理
Text driven for creating and editing images (with source code)
RDKit 安装
[opencvsharp] spot detection barcode decoding image operation image rotation / flip / Zoom perspective transformation image display control demo notes
Association relationship
Drug evaluation index
如何把树结构存储到数据库