当前位置:网站首页>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节点就可以了。

添加节点:

与数组对比:

边栏推荐
- Google Emoji guessing game helps parents guide their children to surf the Internet safely
- 基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习
- JVM Lecture 2: class loading mechanism
- Use field parameters for report translation
- Black eat black? The man cracked the loopholes in the gambling website and "collected wool" for more than 100000 yuan per month
- 如何优雅的复现YOLOv5官方历程(二)——标注并训练自己的数据集
- MySQL八股知识点:从入门到删库
- [cloud native | 17] four network modes of container
- Sliding window -- leetcode solution
- Mysql主从同步及主从同步延迟解决方案
猜你喜欢

Excel VBA: realize automatic drop-down filling formula to the last line

未来大气污染变化模拟

奥特学园ROS笔记--6

MySQL eight knowledge points: from getting started to deleting the database

pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0

Seata两阶段提交AT详解

can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM

Minipcie interface can card solves the problem of industrial computer expanding can channel minipcie can

What points should be paid attention to in the selection of project management system?

Molecular skeleton transition tool -delinker introduction
随机推荐
The landing of tdengine in the GPS and AIS scheduling of Zhongtian steel
提高shuffle操作中的reduce并行度
C语言——字符串函数,内存函数集锦以及模拟实现
Axi protocol (5): burst mechanism of Axi protocol
Two ways to create MySQL database
uniapp小程序框架-一套代码,多段覆盖
五个维度着手MySQL的优化,我和面试官都聊嗨了
There was an unexpected error (type=method not allowed, status=405)
[Luogu] p3919 [template] persistent segment tree 1 (persistent array)
An online accident, I suddenly realized the essence of asynchrony
Ansible tutorial
[mathematical modeling] basic knowledge of MATLAB
基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习
MySQL基础学习
推荐12个免费查找文献的学术网站,建议点赞、收藏!
Excel VBA: realize automatic drop-down filling formula to the last line
Interprocess communication
Redis解决库存超卖问题
Excel VBA:按日期汇总计算输出结果(sumif)
@Autowired注解的原理