当前位置:网站首页>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
边栏推荐
- Oracle Recovery Tools ----oracle数据库恢复利器
- Cartoon: interesting pirate problem (full version)
- Compared with the loss of Wenxin, the performance is improved a lot
- Unicode processing in response of flash interface
- 毫无章法系列
- Cartoon: how to multiply large integers? (next)
- Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
- Size_t 是无符号的
- Tkinter window preload
- Force deduction solution summary 1200 minimum absolute difference
猜你喜欢
mybash
Leetcode daily question: merge two ordered arrays
MySQL之知识点(六)
每日一练:关于日期的一系列
Why is all (()) true and any (()) false?
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
PMP认证需具备哪些条件啊?费用多少啊?
Anaconda中配置PyTorch环境——win10系统(小白包会)
7 pratiques devops pour améliorer la performance des applications
随机推荐
如何保存训练好的神经网络模型(pytorch版本)
Read libco save and restore the on-site assembly code
力扣解法汇总729-我的日程安排表 I
Is it safe to open an account online? What is the general interest rate of securities financing?
如何修改mysql字段为自增长字段
Accuracy of BigDecimal Division
Why is all (()) true and any (()) false?
Server configuration jupyter environment
Disabling and enabling inspections pycharm
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Short the command line via jar manifest or via a classpath file and rerun
To solve the problem of "double click PDF file, pop up", please install Evernote program
統計php程序運行時間及設置PHP最長運行時間
求解为啥all(())是True, 而any(())是FALSE?
Anaconda中配置PyTorch环境——win10系统(小白包会)
SQL Server(2)
GFS distributed file system
Independent development is a way out for programmers
ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
Kafaka技术第一课