当前位置:网站首页>LeetCode 练习——206. 反转链表
LeetCode 练习——206. 反转链表
2022-07-05 17:22:00 【SK_Jaco】
1.题目描述
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
示例 2:
输入:head = [1,2]
输出:[2,1]
示例 3:
输入:head = []
输出:[]
2.解题思路与代码
2.1 解题思路
这道题比较简单,但是也是面试中非常常见的一道题目。题目要求反转链表,可以暴力使用栈解答,但是这样做不够精简在面市场上是得不到分的,并且需要两次遍历。因此使用三个引用变量 pre、next、curr 来指代上一个节点、下一个节点和当前节点。开始将 curr 指向 头节点并从头节点开始进行遍历,next 指向当前节点的下一个节点,然后让当前节点的 next 指向前一个节点,也就是 pre。再往后移动 pre 和 curr,使 pre 指向当前节点 curr ,当前节点 curr 后移指向 next。下面题目示例 1->2->3->4->5->NULL 进行图解:
- 首先 curr 指向头节点 1,pre 和 next 指向 null;
- 然后 next 指向 curr 的下一个节点 2;
- curr 的 next 指向 pre,并且 curr 后移指向 2;
- 重复上面步骤,直到 curr 指向 null,此时返回 pre 指向节点,即反转后的头节点

2.2 代码
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode pre = null;
ListNode next = null;
ListNode curr = head;
while (curr != null) {
next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}
}
2.3 测试结果
通过测试

3.总结
- 这是链表中非常简单的应用,面试中常出现
- 使用 pre、curr、next 三个变量来完成反转,整个流程不难思路整理清楚即可
- 这道题与 剑指 Offer 24. 反转链表 相同
边栏推荐
- Short the command line via jar manifest or via a classpath file and rerun
- Count the running time of PHP program and set the maximum running time of PHP
- Redis+caffeine two-level cache enables smooth access speed
- C # realizes crystal report binding data and printing 3-qr code barcode
- WebApp开发-Google官方教程
- 服务器配置 jupyter环境
- 2022 information system management engineer examination outline
- About JSON parsing function JSON in MySQL_ EXTRACT
- QT console printout
- Disorganized series
猜你喜欢

IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.

mongodb(快速上手)(一)

Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions

MATLAB查阅

Count the running time of PHP program and set the maximum running time of PHP

leetcode每日一练:旋转数组

Knowledge points of MySQL (7)

Anaconda中配置PyTorch环境——win10系统(小白包会)
Database design in multi tenant mode
Tips for extracting JSON fields from MySQL
随机推荐
Redis+caffeine two-level cache enables smooth access speed
C (WinForm) the current thread is not in a single threaded unit, so ActiveX controls cannot be instantiated
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
7. Scala class
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
Independent development is a way out for programmers
統計php程序運行時間及設置PHP最長運行時間
基于YOLOv3的口罩佩戴检测
漫画:寻找股票买入卖出的最佳时机
使用QT设计师界面类创建2个界面,通过按键从界面1切换到界面2
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
Learn about MySQL transaction isolation level
flask接口响应中的中文乱码(unicode)处理
Check the WiFi password connected to your computer
数据访问 - EntityFramework集成
Knowledge points of MySQL (6)
What are the precautions for MySQL group by
Count the running time of PHP program and set the maximum running time of PHP
企业数字化发展中的六个安全陋习,每一个都很危险!