当前位置:网站首页>Linked list (III) - reverse linked list
Linked list (III) - reverse linked list
2022-06-28 06:10:00 【Mu Di%】
Reverse a linked list
Here's the head node of the list
head, Please reverse the list , And return the inverted linked list Power button 206 Reverse a linked list
Example :

Input :head = [1,2,3,4,5]
Output :[5,4,3,2,1]
Tips :
- The number range of nodes in the linked list is [0, 5000]
- -5000 <=
Node.val<= 5000
Double pointer
Observe the structure of the single linked list , Just reverse the pointer of the linked list
- Change the pointer from the position of the node
prePoint tonull,curPoint to the head node- take
curChange the direction ofpre - use
tempStorecurPoint to the next node , For the next reversal curpreMove the pointing back one position , untilcurPoint tonull
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode temp = null;
while (cur != null) {
temp = cur.next;// Save the next node
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
recursive
The operation is repeated in a double pointer , You can also use recursion
- Determine the boundary conditions
class Solution {
public ListNode reverseList(ListNode head) {
// Initialization conditions
return reverse(null, head);
}
public ListNode reverse(ListNode pre, ListNode cur) {
// Jump out of recursive conditions
if (cur == null) {
return pre;
}
ListNode temp = null;
temp = cur.next;// Save the next node first
cur.next = pre;// reverse
return reverse(cur, temp); // Update location
}
}
边栏推荐
- Uni app wechat applet sharing function
- Slow content advertising: the long-term principle of brand growth
- How the third-party libraries in cocoapod reference local header files
- OpenSCAP 简介
- AutoCAD C polyline self intersection detection
- death_ satan/hyperf-validate
- Where is the era bonus for developers?
- Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance
- YYGH-BUG-03
- Scripting and programming languages
猜你喜欢

YYGH-7-用户管理

MySQL(一)——安装

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance

YYGH-BUG-02

AutoCAD C# 多段線小銳角檢測

What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?

链表(三)——反转链表

Binder interview: memory management unit

JDBC学习(一)——实现简单的CRUD操作

Xcode13.3.1 error reported after pod install
随机推荐
Syn retransmission caused by IPVS
The length of pytorch dataloader the difference between epoch and iteration
Install redis on windows and permanently change the password, and integrate redis with the SSM framework
No one can only use foreach to traverse arrays, right?
JDBC学习(一)——实现简单的CRUD操作
idea创建类时自动添加注释
Global country (and region) information JSON data
Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
Yolact++ pytoch environment
6. graduation design temperature and humidity monitoring system (esp8266 + DHT11 +oled real-time upload temperature and humidity data to the public network server and display the real-time temperature
What is the e-commerce conversion rate so abstract?
使用SQL select count distinct查询语句统计数据库中某个字段的唯一值总数量
重载,重写的区别,抽象类,接口的区别
death_satan/hyperf-validate
Oracle fundamentals summary
The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded
Binder interview: memory management unit
OpenSCAP 简介
不会还有人只会用forEach遍历数组吧?
[MySQL] all query tables contain 20million data -- how to optimize SQL