Java 反转带头结点的单链表并显示输出的实现过程
作者:吉吉女王 时间:2022-06-08 18:34:39
注意:要保证已经有Node类和单链表的初始化,这样才能调用反转方法并显示结果。
方法如下:
//Node<T>指泛型结点类
public void reverse2(Node<T> head){
Node<T> p=head.next;
Node<T> q=head.next.next;
head.next=null;
p.next=null;
while(q!=null){
Node<T> temp=q.next;
q.next=p;
p=q;
q=temp;
}
q=this.head;
q.next=p;
/*
//想要直接在方法中遍历输出可以使用这段代码 ***p是反转后的第一个结点***
for(Node<T> qq=p;qq!=null;qq=qq.next){//qq不能为q,因为结束循环后,p是最后一个结点,q是null
System.out.print(qq.data+"\t");
}
*/
}
实现过程如图所示:
在循环之前,先把head以及初始p的next断掉,方便后面的循环;循环中的q.next=p就是重定向,这一步把原来的next指向p;
不用q=q.next来使q向后移动,因为q结点的指针重指向后,q的next为空了,无法正确指向下一个结点。这时用temp结点来做一下过渡,在q指针重指向之前,把q的下一个结点赋给temp,即temp=q.next,在重指向后,用q=temp来向后移动;
p=q;q=temp就是往后移动一个结点,可以继续重复循环。
循环结束时,q为空,p为最后一个结点,使q作为头结点并指向p就完成了反转。
整体代码:
Node类:
public class Node<T>{
public T data;
public Node<T>next;
public Node(T data,Node<T>next){
this.data=data;
this.next=next;
}
public Node(){
this(null,null);
}
public String toString(){
return this.data.toString();
}
}
单链表类:
public class SinglyList<T> {
public Node<T> head;
public SinglyList(){
this.head=new Node<T>();
}
public SinglyList(T[] values){
this();
Node<T> rear=this.head;
for(int i=0;i<values.length;i++)
{
rear.next=new Node<T>(values[i],null);
rear=rear.next;
}
}
//输出方法
public String toString(){
String str=this.getClass().getName()+"(";
for(Node<T>p=this.head.next;p!=null;p=p.next)
{
str+=p.data.toString();
if(p.next!=null)
str+=",";
}
return str+=")";
}
//反转方法
public void reverse2(Node<T> head){
Node<T> p=head.next;
Node<T> q=head.next.next;
head.next=null;
p.next=null;
while(q!=null){
Node<T> temp=q.next;
q.next=p;
p=q;
q=temp;
}
q=this.head;
q.next=p;
/*
//想要直接在方法中遍历输出可以使用这段代码 ***p是反转后的第一个结点***
for(Node<T> qq=p;qq!=null;qq=qq.next){//qq不能为q,因为结束循环后,p是最后一个结点,q是null
System.out.print(qq.data+"\t");
}
*/
}
public static void main(String[] args) {
Integer[] a={1,2,4,5,7};
SinglyList<Integer> sl=new SinglyList<>(a);
System.out.println(sl.toString());
sl.reverse2(sl.head);
System.out.println(sl.toString());
}
}
输出结果:
来源:https://blog.csdn.net/m0_57414485/article/details/121265470
标签:Java,反转,带头结点,单链表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#中实现图片灰度化技术详解
2023-06-26 22:25:56
![](https://img.aspxhome.com/file/2023/3/88373_0s.png)
Devexpress treelist 简介
2023-02-13 23:07:56
在 C# 中使用 Span<T> 和 Memory<T> 编写高性能代码的详细步骤
2022-06-06 05:06:41
![](https://img.aspxhome.com/file/2023/8/96388_0s.png)
从零开始学springboot整合feign跨服务调用的方法
2023-05-15 18:30:22
![](https://img.aspxhome.com/file/2023/5/81895_0s.png)
C#操作Excel相关方法总结
2021-06-22 12:00:48
SpringBoot 整合 Shiro 密码登录与邮件验证码登录功能(多 Realm 认证)
2023-06-23 02:45:28
Android ListView 和ScroolView 出现onmeasure空指针的解决办法
2021-11-25 18:19:03
详解Jmeter线程组的设置方法
2021-11-14 10:10:56
![](https://img.aspxhome.com/file/2023/2/78362_0s.png)
Android来电拦截的实现方法
2023-10-09 16:42:20
![](https://img.aspxhome.com/file/2023/5/108265_0s.jpg)
C#绘制中国象棋棋盘
2021-05-27 15:08:44
![](https://img.aspxhome.com/file/2023/1/94551_0s.jpg)
Netty实战入门教程之 什么是Netty
2023-10-14 01:08:09
![](https://img.aspxhome.com/file/2023/9/78939_0s.png)
基于Apache组件分析对象池原理的实现案例分析
2023-11-05 16:55:57
![](https://img.aspxhome.com/file/2023/2/99742_0s.png)
Swing常用组件之单选按钮和复选框
2023-02-15 13:35:05
![](https://img.aspxhome.com/file/2023/1/92721_0s.png)
C#实现身份证号码验证的方法
2022-11-08 09:15:34
WinForm实现状态栏跑马灯效果的方法示例
2023-07-16 19:37:33
在C#中使用二叉树实时计算海量用户积分排名的实现详解
2023-05-15 07:51:12
![](https://img.aspxhome.com/file/2023/1/79101_0s.png)
Android Studio 运行按钮灰色的完美解决方法
2023-08-16 05:59:42
![](https://img.aspxhome.com/file/2023/4/89534_0s.png)
解决Map集合使用get方法返回null抛出空指针异常问题
2023-11-25 00:14:00
![](https://img.aspxhome.com/file/2023/8/59848_0s.png)
利用logback filter过滤某个类 屏蔽某个类
2023-07-25 20:38:08
![](https://img.aspxhome.com/file/2023/3/108443_0s.png)
深入理解MyBatis中的一级缓存与二级缓存
2022-05-25 09:41:34
![](https://img.aspxhome.com/file/2023/7/66057_0s.png)