当前位置:网站首页>206、反转链表(链表)
206、反转链表(链表)
2022-04-23 10:12:00 【Popuessing's Jersey】
题意:反转一个单链表。
示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
方法一:双指针法
public class Fanzhuanlianbiao {
static class ListNode{
int val;//数据:节点数据
ListNode next;//对象,引用下一个数据对象
//链表类的构造方法
ListNode(int val){
this.val = val;
}
}
public void print (ListNode head){
while(head!=null){
System.out.print(head.val+" ");
head = head.next;
}
}
public ListNode reverseList(ListNode head){
//定义一个临时变量保存当前节点的下一个节点
ListNode temp;
//定义前驱节点
ListNode pre = null;
//定义当前节点
ListNode cur = head;
while(cur!=null){
//临时变量记录当前节点的下一个节点
temp=cur.next;
//当前节点指向前驱结点
cur.next = pre;
//前驱结点后移一位
pre = cur;
//当前节点后移一位
cur = temp;
}
return pre;
}
public static void main(String[] args) {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
Fanzhuanlianbiao fanzhuanlianbiao = new Fanzhuanlianbiao();
ListNode node = node1;
fanzhuanlianbiao.print(node);
System.out.println();
ListNode res =fanzhuanlianbiao.reverseList(node1);
fanzhuanlianbiao.print(res);
}
}
输出结果:
12345
54321
方法二:递归法
思路跟双指针类似,注意递归的终点和递归的变量
//递归法
public ListNode reverseList(ListNode head){
return reverse(null,head);
}
private ListNode reverse(ListNode pre,ListNode cur){
//到达递归终点,返回pre
if (cur==null){
return pre;
}
//定义临时节点temp保存当前节点下一个节点
ListNode temp = null;
temp = cur.next;
//反转(当前节点指向前一个节点
cur.next = pre;
return reverse(cur,temp);
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121408850
边栏推荐
- Zhengda international explains what the Dow Jones industrial index is?
- 1D / 1D dynamic programming learning summary
- DBA常用SQL语句(2)— SGA和PGA
- Chapter 1 Oracle database in memory related concepts (im-1.1)
- Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
- 自定义登录失败处理
- 2022年制冷与空调设备运行操作考试练习题及模拟考试
- JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
- DBA常用SQL语句(4)- Top SQL
- ansible playbook语法和格式 自动化云计算
猜你喜欢

工业元宇宙平台规划与建设

C语言——自定义类型

Sim Api User Guide(6)

2022 mobile crane driver test question bank simulation test platform operation

Operation of 2022 tea artist (primary) test question simulation test platform

Juc并发编程09——Condition实现源码分析

Nvidia最新三维重建技术Instant-ngp初探

JVM——》常用参数

lnmp的配置

JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
随机推荐
Custom login failure handling
通过流式数据集成实现数据价值(5)- 流分析
计算机网络安全实验二|DNS协议漏洞利用实验
Sim Api User Guide(8)
第三章 启用和调整IM列存储的大小(IM-3.1)
Juc并发编程07——公平锁真的公平吗(源码剖析)
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
【无标题】
构建元宇宙时代敏捷制造的九种能力
Jerry's factors that usually affect CPU performance test results are: [article]
Yarn资源调度器
shell脚本免交互
Rain produces hundreds of valleys, and all things grow
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
Realizing data value through streaming data integration (4) - streaming data pipeline
formatTime时间戳格式转换
域名和IP地址的联系
Shell script interaction free
MapReduce核心和基础Demo
ansible 云计算 自动化