当前位置:网站首页>LeetCode链表问题——206.反转链表(一题一文学会链表)
LeetCode链表问题——206.反转链表(一题一文学会链表)
2022-07-26 05:05:00 【十八岁讨厌Java】
一、题目描述:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

二、代码
// 双指针
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode cur = head;
ListNode temp = null;
while (cur != null) {
temp = cur.next;// 保存下一个节点
cur.next = prev;
prev = cur;
cur = temp;
}
return prev;
}
}三、了解链表
什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。链接的入口节点称为链表的头结点也就是head。
单链表:
双链表:
每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
双链表 既可以向前查询也可以向后查询。

循环链表:
循环链表,顾名思义,就是链表首尾相连。循环链表可以用来解决约瑟夫环问题。

链表的存储方式:
数组是在内存中是连续分布的,但是链表在内存中可不是连续分布的。
链表是通过指针域的指针链接在内存中各个节点。
所以链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。

这个链表起始节点为2, 终止节点为7, 各个节点分布在内存的不同地址空间上,通过指针串联在一起。
删除节点:
只要将C节点的next指针 指向E节点就可以了。

添加节点:

与数组对比:

边栏推荐
- Interprocess communication
- Mysql主从同步及主从同步延迟解决方案
- Seata submits at details in two stages
- 9 best project set management tools
- Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month
- 推荐12个免费查找文献的学术网站,建议点赞、收藏!
- Excel VBA:实现自动下拉填充公式至最后一行
- 注解@Autowired如何自动装配
- 【ACWing】2983. 玩具
- List converted to tree real use of the project
猜你喜欢

Excel VBA: summarize calculation output results by date (SUMIF)

Date and time function of MySQL function summary

An online accident, I suddenly realized the essence of asynchrony
![[mathematical modeling] basic knowledge of MATLAB](/img/b5/6bd507a149968931ab1c4644f2c08b.png)
[mathematical modeling] basic knowledge of MATLAB

公交站间的距离 : 简单模拟题

AXI协议(4):AXI通道上的信号

Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
![[pytorch] install torch 1.8.1 and check whether torch version and GPU are available](/img/97/078c72729a29675939a84895b5ab86.png)
[pytorch] install torch 1.8.1 and check whether torch version and GPU are available

JVM Lecture 2: class loading mechanism

五个维度着手MySQL的优化,我和面试官都聊嗨了
随机推荐
To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
Switch and router technology: dynamic routing protocol, rip routing protocol and OSPF routing protocol
How to connect tdengine through idea database management tool?
遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
How many holes have you stepped on in BigDecimal?
阿里云工业视觉智能工程师ACP认证——备考
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
The pit of history can only be filled up as far as possible
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
When AQS wakes up the thread, I understand why it traverses from the back to the front
Use field parameters for report translation
【Leetcode】493. Reverse Pairs
An online accident, I suddenly realized the essence of asynchrony
汉字风格迁移篇---通过生成对抗网络学习一对多程式化汉字的转换和生成
AQS唤醒线程的时候为什么从后向前遍历,我懂了
嵌入式分享合集20
Ggjj, do you have a look at this problem? Does caching cause cross domain problems?
Good at C (summer vacation daily question 6)
如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
Install nccl \ mpirun \ horovod \ NVIDIA tensorflow (3090ti)