当前位置:网站首页>链表(三)——反转链表
链表(三)——反转链表
2022-06-28 06:04:00 【木棣%】
给你单链表的头节点
head,请你反转链表,并返回反转后的链表示例 :

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
提示:
- 链表中节点的数目范围是 [0, 5000]
- -5000 <=
Node.val<= 5000
双指针
观察单链表的结构,只需要反转链表指针的指向
- 从头结点的位置开始改变指针指向
pre指向null,cur指向头结点- 将
cur的指向改为pre - 用
temp储存cur指向的下一个节点,便于下一次反转 curpre指向都后移一个位置,直至cur指向null
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode temp = null;
while (cur != null) {
temp = cur.next;// 保存下一个节点
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
递归
在双指针中操作是重复的,也可以使用递归
- 确定好边界条件
class Solution {
public ListNode reverseList(ListNode head) {
// 初始化条件
return reverse(null, head);
}
public ListNode reverse(ListNode pre, ListNode cur) {
// 跳出递归的条件
if (cur == null) {
return pre;
}
ListNode temp = null;
temp = cur.next;// 先保存下一个节点
cur.next = pre;// 反转
return reverse(cur, temp); // 更新位置
}
}
边栏推荐
- 2022年全国职业院校技能大赛“网络安全”竞赛试题官方答案
- Jenkins continues integration 2
- Maskrcnn,fast rcnn, faster rcnn优秀视频
- Small ball playing
- Valueerror: iterative over raw text documents expected, string object received
- 5g network overall architecture
- Idea automatically adds comments when creating classes
- Caused by: com. fasterxml. jackson. databind. Exc.invalidformatexception: exception resolution
- ES9023音频解码芯片的工作原理
- Filecoin黑客松开发者大赛
猜你喜欢

慢内容广告:品牌增长的长线主义

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

YYGH-6-微信登录

Filecoin黑客松开发者大赛

Yygh-6-wechat login

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

The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded

Lenovo hybrid cloud Lenovo xcloud, new enterprise IT service portal

ES9023音频解码芯片的工作原理

Capacity scheduling absolute value configuration queue usage and pit avoidance
随机推荐
Slow content advertising: the long-term principle of brand growth
What is the e-commerce conversion rate so abstract?
Caused by: com. fasterxml. jackson. databind. Exc.invalidformatexception: exception resolution
Xcode13.3.1 项目执行pod install后报错
Cryptography notes
What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?
原动力×云原生正发声 降本增效大讲堂
windows上安装redis并永久修改密码,及ssm框架集成redis
脚本语言和编程语言
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException:异常解决
【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images
Mosaic data enhanced mosaic
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance
1404. number of steps to reduce binary representation to 1
全球国家(和地区)信息JSON数据
Apple MDM bypass jailfree bypass MDM configuration lock free
bash install. SH ******** error
Official answers to the "network security" competition questions of the 2022 national vocational college skills competition
基于Kotlin+JetPack实现的MVVM框架的示例
移动广告发展动向:撬动存量,精细营销