当前位置:网站首页>206.反转链表
206.反转链表
2022-07-01 03:28:00 【兀坐晴窗独饮茶】

思路一 改变指针方向

主体思路 :遍历所有节点, 改变节点的指向方向
- 初始状态 : last 指向 null, head 指向头结点

- 定义一个 nextHead 节点, 用于存储 head.next 的节点, 否则方向一改变, head.next 节点就丢失了

- 然后改变 head的边的方向 , head.next = last

- last 和 head 向前移动一步

- 然后循环上述过程即可
class Solution {
public ListNode reverseList(ListNode head) {
ListNode last = null;
while(head != null) {
// 保存head.next , 原因是 边的方向改变时, head.next 的节点会丢失, 所以要提前保存
ListNode nextHead = head.next;
// 改变边的方向
head.next = last;
// last 和 head 向前移动一步
last = head;
head = nextHead;
}
return last;
}
}
边栏推荐
- [TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
- 访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
- C语言的sem_t变量类型
- Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)
- 【伸手党福利】JSONObject转String保留空字段
- Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
- Feature pyramid networks for object detection
- Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
- [ta- frost wolf \u may- hundred people plan] 2.2 model and material space
- MFC window scroll bar usage
猜你喜欢

Sort linked list (merge sort)

复习专栏之---消息队列

Promql select time series
![Pyramid scene parsing network [pspnet] thesis reading](/img/05/4645c8a595083479dee6835620335d.png)
Pyramid scene parsing network [pspnet] thesis reading

Binary tree god level traversal: Morris traversal

Blueprism registration, download and install -rpa Chapter 1

Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination

Unexpected token o in JSON at position 1, JSON parsing problem

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

Bilinear upsampling and f.upsample in pytorch_ bilinear
随机推荐
使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
Valentine's Day is nothing.
【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎
5. [WebGIS practice] software operation - service release and permission management
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
【TA-霜狼_may-《百人计划》】2.2 模型与材质空间
[EI conference] the Third International Conference on nanomaterials and nanotechnology in 2022 (nanomt 2022)
Unexpected token o in JSON at position 1 ,JSON解析问题
Use of comment keyword in database
The combination of applet container technology and IOT
241. Design priorities for operational expressions
Asgnet paper and code interpretation 2
214. minimum palindrome string
【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
Jeecgboot output log, how to use @slf4j
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
8. string conversion integer (ATOI)
SEM of C language_ Tvariable type
Future of NTF and trends in 2022
Leetcode 128 longest continuous sequence (hash set)