当前位置:网站首页>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
}
}
边栏推荐
- Lenovo hybrid cloud Lenovo xcloud, new enterprise IT service portal
- YYGH-7-用户管理
- Sklearn Feature Engineering (summary)
- 从0配置redis
- ThreadLocal
- YYGH-8-预约挂号
- 移动广告发展动向:撬动存量,精细营销
- Official answers to the "network security" competition questions of the 2022 national vocational college skills competition
- Enum
- mysql常用函数
猜你喜欢

Yygh-6-wechat login

Camx架构开UMD、KMD log以及dump图的方式

Ethereum Classic的难度计算|猿创征文

Filecoin hacker song developer competition

YYGH-6-微信登录

【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images

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

mac下安装多个版本php并且进行管理

链表(三)——反转链表

Use of JDBC
随机推荐
Yygh-8-appointment registration
No one can only use foreach to traverse arrays, right?
Oracle fundamentals summary
Configure redis from 0
pytorch详解
Slow content advertising: the long-term principle of brand growth
基本类型和包装类的区别
OpenSCAP 简介
[untitled]
MySQL(一)——安装
socke. IO long connection enables push, version control, and real-time active user statistics
全球国家(和地区)信息JSON数据
The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded
Install fmpefg
Oracle condition, circular statement
Differences between overloads, rewrites, abstract classes and interfaces
API learning of OpenGL (2006) glclientactivetexture
AutoCAD C polyline small acute angle detection
cocoapod中的第三方库怎么引用本地头文件
Mosaic data enhanced mosaic