Java实现单向链表反转
作者:hanxue1122 时间:2023-11-18 01:03:11
本文实例为大家分享了Java实现单向链表反转的具体代码,供大家参考,具体内容如下
1、实现代码
public class LinkedListTest {
public static void main(String[] args) {
Node A = new Node("A");
Node B = new Node("B");
Node C = new Node("C");
Node D = new Node("D");
Node E = new Node("E");
Node F = new Node("F");
A.next = B;
B.next = C;
C.next = D;
D.next = E;
E.next = F;
print(A);
LinkedListReversor reversor = LinkedListReversor.RECURSION;
System.out.println(reversor.getStrategy() + ":");
Node tmp = reversor.execute(A);
print(tmp);
reversor = LinkedListReversor.NO_RECURSION;
System.out.println(reversor.getStrategy() + ":");
print(reversor.execute(tmp));
}
private static void print(Node node) {
while (node != null) {
System.out.print(node.value);
node = node.next;
if (node != null) {
System.out.print("->");
} else {
System.out.println();
}
}
}
}
class Node {
public String value;
public Node next;
public Node(String value) {
this.value = value;
}
}
enum LinkedListReversor {
RECURSION("递归") {
@Override
public Node execute(Node node) {
Node prev = null;
if (node == null || node.next == null) {
prev = node;
} else {
Node tmp = execute(node.next);
node.next.next = node;
node.next = null;
prev = tmp;
}
return prev;
}
},
NO_RECURSION("非递归") {
@Override
public Node execute(Node node) {
Node prev = null;
while (node != null) {
Node tmp = node;
node = node.next;
tmp.next = prev;
prev = tmp;
}
return prev;
}
};
private String strategy;
private LinkedListReversor(String strategy) {
this.strategy = strategy;
}
public abstract Node execute(Node node);
public String getStrategy() {
return strategy;
}
}
2、实现思路
递归:从尾部开始处理
非递归:从头部开始处理
来源:https://www.cnblogs.com/hanxue112253/p/8533426.html
标签:java,单向链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android10 隐藏SystemUI锁屏下的多用户图标的示例代码
2023-12-12 03:14:26
![](https://img.aspxhome.com/file/2023/2/139582_0s.png)
面试初级Java开发问到Arrays
2023-11-27 05:40:04
Java中超详细this与super的概念和用法
2021-05-25 19:45:50
![](https://img.aspxhome.com/file/2023/1/94201_0s.png)
Java 合并多个MP4视频文件
2022-08-23 12:09:19
Java Timer使用讲解
2023-11-28 20:30:33
Android绘制钟表的方法
2023-09-01 13:18:03
![](https://img.aspxhome.com/file/2023/5/128985_0s.gif)
Java11 发布前抓紧掌握这些新特性
2022-02-08 13:20:58
![](https://img.aspxhome.com/file/2023/8/65578_0s.jpg)
Java中抓取 Thread Dumps 的方式汇总
2021-08-09 23:20:26
![](https://img.aspxhome.com/file/2023/6/95936_0s.png )
基于spring @Cacheable 注解的spel表达式解析执行逻辑
2023-07-03 19:46:45
![](https://img.aspxhome.com/file/2023/0/113440_0s.jpg)
C#使用async和await实现异步编程
2023-08-23 18:32:52
![](https://img.aspxhome.com/file/2023/0/108760_0s.png)
修改Maven settings.xml 后配置未生效的解决
2023-07-06 22:17:03
c语言定时器示例分享
2023-11-04 09:44:52
Android开发从相机或相册获取图片裁剪
2022-11-09 01:52:17
![](https://img.aspxhome.com/file/2023/2/137642_0s.png)
自定义log4j日志文件命名规则说明
2021-11-21 16:55:51
![](https://img.aspxhome.com/file/2023/1/71591_0s.png)
SpringBoot根据目录结构自动配置Url前缀方式
2023-01-31 14:04:29
![](https://img.aspxhome.com/file/2023/7/113457_0s.png)
教你如何用好 Java 中的枚举
2022-11-03 10:31:21
C#设置自定义文件图标实现双击启动(修改注册表)
2023-08-21 08:43:46
Android使用SoundPool播放音效
2021-11-24 02:32:36
![](https://img.aspxhome.com/file/2023/4/139014_0s.jpg)
C++实现简单酒店管理系统
2021-07-31 13:52:40
![](https://img.aspxhome.com/file/2023/9/122579_0s.jpg)
C#编程实现统计文件夹内文件和隐藏文件的方法示例
2022-09-29 16:09:38
![](https://img.aspxhome.com/file/2023/3/74823_0s.jpg)