当前位置:网站首页>Leetcode exercise - 206 Reverse linked list
Leetcode exercise - 206 Reverse linked list
2022-07-05 17:50:00 【SK_ Jaco】
1. Title Description
Here's the head node of the list head , Please reverse the list , And return the inverted linked list .
Example 1:
Input :head = [1,2,3,4,5]
Output :[5,4,3,2,1]
Example 2:
Input :head = [1,2]
Output :[2,1]
Example 3:
Input :head = []
Output :[]
2. Problem solving ideas and codes
2.1 Their thinking
This question is relatively simple , But it is also a very common topic in the interview . The title requires reversing the linked list , You can use the stack violently to solve , But this is not simple enough, and you can't get points in the face market , And you need to traverse twice . So use three reference variables pre、next、curr To refer to the previous node 、 Next node and current node . Begin to curr Point to Head node and traverse from the head node ,next Point to the next node of the current node , Then let the current node next Point to previous node , That is to say pre. Move back pre and curr, send pre Point to the current node curr , Current node curr Move back to next. Examples of topics below 1->2->3->4->5->NULL To illustrate :
- First curr Point to the head node 1,pre and next Point to null;
- then next Point to curr The next node of 2;
- curr Of next Point to pre, also curr Move back to 2;
- Repeat the above steps , until curr Point to null, Return at this time pre Point to the node , That is, the inverted head node
2.2 Code
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 test result
Pass the test
3. summary
- This is a very simple application in linked lists , Often appear in interviews
- Use pre、curr、next Three variables to complete the inversion , The whole process is not difficult, just sort out the ideas clearly
- This problem is related to The finger of the sword Offer 24. Reverse a linked list identical
边栏推荐
- 使用QT设计师界面类创建2个界面,通过按键从界面1切换到界面2
- 2022新版PMP考试有哪些变化?
- c#图文混合,以二进制方式写入数据库
- Configure pytorch environment in Anaconda - win10 system (small white packet meeting)
- EPM相关
- 数据访问 - EntityFramework集成
- Cartoon: looking for the k-th element of an unordered array (Revised)
- PMP认证需具备哪些条件啊?费用多少啊?
- ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
- Mongodb (quick start) (I)
猜你喜欢
How awesome is the architecture of "12306"?
VBA drives SAP GUI to realize office automation (II): judge whether elements exist
7 pratiques devops pour améliorer la performance des applications
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
EPM related
Count the running time of PHP program and set the maximum running time of PHP
Zabbix
flask接口响应中的中文乱码(unicode)处理
解决“双击pdf文件,弹出”请安装evernote程序
随机推荐
Independent development is a way out for programmers
Operation before or after Teamcenter message registration
企业数字化发展中的六个安全陋习,每一个都很危险!
Flask solves the problem of CORS err
北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员
LeetCode每日一题:合并两个有序数组
Cartoon: interesting [pirate] question
"Xiaodeng in operation and maintenance" is a single sign on solution for cloud applications
统计php程序运行时间及设置PHP最长运行时间
Cartoon: how to multiply large integers? (next)
Thesis reading_ Chinese NLP_ LTP
EPM相关
How to save the trained neural network model (pytorch version)
力扣解法汇总729-我的日程安排表 I
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Oracle recovery tools -- Oracle database recovery tool
Thesis reading_ Medical NLP model_ EMBERT
Tita performance treasure: how to prepare for the mid year examination?
排错-关于clion not found visual studio 的问题
CVPR 2022 best student paper: single image estimation object pose estimation in 3D space