当前位置:网站首页>Leetcode linked list problem -- 24. Exchange the nodes in the linked list in pairs (learn the linked list with one question and one article)
Leetcode linked list problem -- 24. Exchange the nodes in the linked list in pairs (learn the linked list with one question and one article)
2022-07-26 20:54:00 【18-year-old hates Java】
One 、 Title Description :
24. Two or two exchange nodes in a linked list
Medium difficulty 1478 Switch to English to receive dynamic feedback
I'll give you a list , Two or two exchange the adjacent nodes , And return the head node of the linked list after exchange . You must complete this problem without modifying the value inside the node ( namely , Only node switching can be performed ).

Two 、 Code
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode prev = dummyNode;
while (prev.next != null && prev.next.next != null) {
ListNode temp = head.next.next; // cache next
prev.next = head.next; // take prev Of next Change it to head Of next
head.next.next = head; // take head.next(prev.next) Of next, Point to head
head.next = temp; // take head Of next Connect the cached temp
prev = head; // Stepping 1 position
head = head.next; // Stepping 1 position
}
return dummyNode.next;
}
}3、 ... and 、 Understand linked list
What is a linked list , A linked list is a linear structure connected in series by pointers , Each node consists of two parts , One is the data field and the other is the pointer field ( Holds a pointer to the next node ), The pointer field of the last node points to null( A null pointer means ). The entry node of the link is called the head node of the linked list head.
Single chain list :
Double linked list :
Each node has two pointer fields , One points to the next node , One points to the previous node .
Double linked list You can query forward or backward .

Circular linked list :
Circular linked list , seeing the name of a thing one thinks of its function , The linked list is connected end to end . Circular list can be used to solve the Joseph Ring problem .

How to store linked lists :
Arrays are continuously distributed in memory , But linked lists are not continuously distributed in memory .
The linked list links the nodes in the memory through the pointer of the pointer field .
So the nodes in the linked list are not continuously distributed in memory , It's scattered on some address in memory , The allocation mechanism depends on the memory management of the operating system .
Delete node :
As long as C Node next The pointer Point to E Node is OK .

Add a node :

Compare with array :

边栏推荐
- BGP routing black hole and anti ring
- [pyqt5 basic control usage analysis]
- Sword finger offer46 translates numbers into strings
- [interview brush 101] dynamic planning 1
- Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
- 英国德国相继推出5G商用服务,华为成幕后功臣
- 执行上下文与词法环境
- NVIDIA canvas first experience~
- Buu brush inscription 4
- Shell综合应用案例,归档文件
猜你喜欢

Discussion on loan agreement mode with NFT as collateral

Buu brush inscription 2

BGP routing black hole and anti ring
![Installation and configuration of [basic services] [database] Clickhouse](/img/fe/5c24e4c3dc17a6a96985e4fe97024e.png)
Installation and configuration of [basic services] [database] Clickhouse

执行上下文与词法环境

St table, weighted and search set

Houdini 求中点,点连成线

Arpspoof installation and use

LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)

Message queue -- the problem introduced: repeated consumption & sequential consumption & distributed transactions
随机推荐
让华为吃了败诉的CNEX Labs到底有何来头?
连接池快速入门
A super simple neural network code with 5 coordinates for one layer node training
Opencv DNN deployment onnx model
What is the origin of CNEX labs, which let Huawei lose the lawsuit?
【PyQt5基本控件使用解析】
【面试必刷101】动态规划1
opencv dnn部署onnx模型
Correlation analysis between [machine learning] variables
Swiftui 4's new function of real-time access to click location.Ontapgeture {location in} (tutorial with source code)
[interview brush 101] dynamic planning 1
Why can ThreadLocal achieve thread isolation?
CentOS7关于Oracle RAC 11GR2部署磁盘分区问题
Chapter 2: encounter obstacles! Bypass WAF filtering! [SQL injection attack]
消息队列——引入的问题:重复消费&顺序消费&分布式事务
Basic configuration and aggregation of BGP
Do employees have to compensate the company for losses when they resign? The 34 year old captain resigned and was claimed 10.66 million yuan by the company
Face recognition and fingerprint recognition are weak? Pentagon develops long-distance heartbeat recognition
7.25模拟赛总结
[record of question brushing] 22. bracket generation