当前位置:网站首页>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
边栏推荐
- 力扣解法汇总729-我的日程安排表 I
- 得知女儿被猥亵,35岁男子将对方打至轻伤二级,法院作出不起诉决定
- Is it safe to open an account online? What is the general interest rate of securities financing?
- GFS distributed file system
- VBA drives SAP GUI to realize office automation (II): judge whether elements exist
- ITK Example
- Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
- Mask wearing detection based on yolov3
- Cmake tutorial step6 (add custom commands and generate files)
- Tita 绩效宝:如何为年中考核做准备?
猜你喜欢
EPM相关
提高应用程序性能的7个DevOps实践
Daily exercise: a series of dates
Mongodb (quick start) (I)
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
Leetcode daily question: the first unique character in the string
flask接口响应中的中文乱码(unicode)处理
What are the requirements for PMP certification? How much is it?
论文阅读_医疗NLP模型_ EMBERT
Leetcode daily practice: rotating arrays
随机推荐
C # mixed graphics and text, written to the database in binary mode
Zabbix
How to modify MySQL fields as self growing fields
漫画:有趣的海盗问题 (完整版)
每日一练:关于日期的一系列
Interpretation: how to deal with the current security problems faced by the Internet of things?
Knowledge points of MySQL (6)
ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
Read libco save and restore the on-site assembly code
Tita performance treasure: how to prepare for the mid year examination?
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
mybash
删除数组中的某几个元素
Abnormal recovery of virtual machine Oracle -- Xi Fenfei
企业数字化发展中的六个安全陋习,每一个都很危险!
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
mongodb(快速上手)(一)
证券网上开户安全吗?证券融资利率一般是多少?
To solve the problem of "double click PDF file, pop up", please install Evernote program
Disabling and enabling inspections pycharm