当前位置:网站首页>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
pre
Point tonull
,cur
Point to the head node- take
cur
Change the direction ofpre
- use
temp
Storecur
Point to the next node , For the next reversal cur
pre
Move the pointing back one position , untilcur
Point 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
}
}
边栏推荐
- socke. IO long connection enables push, version control, and real-time active user statistics
- Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
- Filecoin hacker song developer competition
- cocoapod中的第三方库怎么引用本地头文件
- 深度学习19种损失函数
- YYGH-7-用户管理
- Slow content advertising: the long-term principle of brand growth
- pytorch dataloader的长度 epoch与iteration的区别
- Introduction to openscap
- Alert pop-up processing in Web Automation
猜你喜欢
5g network overall architecture
19 fonctions de perte d'apprentissage profond
YYGH-BUG-03
Slow content advertising: the long-term principle of brand growth
What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?
YYGH-BUG-02
报错--解决core-js/modules/es.error.cause.js报错
Iframe switching in Web Automation
YYGH-6-微信登录
YYGH-8-预约挂号
随机推荐
基于Kotlin+JetPack实现的MVVM框架的示例
D3D11_ Chili_ Tutorial (3): design a bindable/drawable system
自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
[staff] arpeggio mark
借助nz-pagination中的let-total解析ng-template
Enum
简单手写debounce函数
OpenSCAP 简介
从0配置redis
Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
Where is the era bonus for developers?
Iframe switching in Web Automation
Maskrcnn, fast RCNN, fast RCNN excellent video
Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
API learning of OpenGL (2006) glclientactivetexture
Install redis on windows and permanently change the password, and integrate redis with the SSM framework
MySQL(二)——基本操作
预训练模型参数不匹配
Introduction to openscap
Jenkins continues integration 2