方法1:反转打印(但是会改变链表结构,不建议)
https://blog.csdn.net/weixin_43736084/article/details/101939789
方法2:存入栈中,在出栈
public static void reversePrint(HeroNode head
){
if (head
.next
== null
){
return;
}
Stack
<HeroNode> stack
= new Stack<>();
HeroNode cur
= head
.next
;
while(cur
!= null
){
stack
.push(cur
);
cur
= cur
.next
;
}
while (stack
.size()>0){
System
.out
.println(stack
.pop());
}
}
源代码
package DataStructures
.LinkedList
;
import java
.util
.Stack
;
public class SingleLinedListDemo {
public static void main(String
[] args
) {
HeroNode hero1
= new HeroNode(1, "宋江", "及时雨");
HeroNode hero2
= new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3
= new HeroNode(3, "吴用", "智多星");
HeroNode hero4
= new HeroNode(4, "林冲", "豹子头");
SingleLinkedList linkedList
= new SingleLinkedList();
linkedList
.addByOrder(hero1
);
linkedList
.addByOrder(hero4
);
linkedList
.addByOrder(hero2
);
linkedList
.addByOrder(hero3
);
reversePrint(linkedList
.getHead());
}
public static int getLength(HeroNode head
){
if (head
.next
== null
){
return 0;
}
int length
= 0;
HeroNode cur
= head
.next
;
while(cur
!=null
){
length
++;
cur
= cur
.next
;
}
return length
;
}
public static HeroNode
fidLastIndexNode(HeroNode head
, int index
){
if (head
.next
== null
){
return null
;
}
int size
= getLength(head
);
if (index
<=0 || index
> size
){
return null
;
}
HeroNode temp
= head
.next
;
for (int i
=0; i
<size
- index
; i
++){
temp
= temp
.next
;
}
return temp
;
}
public static void reverseList(HeroNode head
){
if (head
.next
== null
|| head
.next
.next
== null
){
return ;
}
HeroNode cur
= head
.next
;
HeroNode next
= null
;
HeroNode reverseHead
= new HeroNode(0,"","");
while(cur
!= null
){
next
= cur
.next
;
cur
.next
= reverseHead
.next
;
reverseHead
.next
= cur
;
cur
= next
;
}
head
.next
= reverseHead
.next
;
}
public static void reversePrint(HeroNode head
){
if (head
.next
== null
){
return;
}
Stack
<HeroNode> stack
= new Stack<>();
HeroNode cur
= head
.next
;
while(cur
!= null
){
stack
.push(cur
);
cur
= cur
.next
;
}
while (stack
.size()>0){
System
.out
.println(stack
.pop());
}
}
}
class SingleLinkedList{
private HeroNode head
= new HeroNode(0,"","");
public HeroNode
getHead() {
return head
;
}
public void add(HeroNode heroNode
){
HeroNode temp
= head
;
while(true){
if(temp
.next
== null
){
break;
}
temp
= temp
.next
;
}
temp
.next
= heroNode
;
}
public void addByOrder(HeroNode heroNode
){
HeroNode temp
= head
;
boolean flag
= false;
while (true) {
if (temp
.next
== null
) {
break;
}
if (temp
.next
.no
> heroNode
.no
) {
break;
}else if (temp
.next
.no
== heroNode
.no
){
flag
= true;
break;
}
temp
= temp
.next
;
}
if(flag
){
System
.out
.printf("人物编号%d 已经存在,不能添加\n",heroNode
.no
);
} else {
heroNode
.next
= temp
.next
;
temp
.next
= heroNode
;
}
}
public void updata(HeroNode newHeroNode
){
if (head
.next
== null
){
System
.out
.println("链表为空");
return;
}
HeroNode temp
= head
.next
;
boolean flag
= false;
while (true){
if (temp
== null
){
break;
}
if (temp
.no
== newHeroNode
.no
){
flag
= true;
break;
}
temp
= temp
.next
;
}
if (flag
){
temp
.name
= newHeroNode
.name
;
temp
.nickName
= newHeroNode
.nickName
;
}else {
System
.out
.printf("没有找到编号:%d 的节点,不能修改",newHeroNode
.no
);
}
}
public void delete(int no
){
if (head
.next
== null
){
System
.out
.println("链表为空,不能删除");
return;
}
HeroNode temp
= head
;
boolean flag
= false;
while (true){
if (temp
.next
== null
){
break;
}
if (temp
.next
.no
== no
){
flag
= true;
break;
}
temp
= temp
.next
;
}
if (flag
){
temp
.next
= temp
.next
.next
;
}else {
System
.out
.printf("链表里没有no为:%d 的节点\n",no
);
}
}
public void list(){
if (head
.next
== null
){
return;
}
HeroNode temp
= head
.next
;
while(true){
if (temp
== null
){
break;
}
System
.out
.println(temp
);
temp
= temp
.next
;
}
}
}
class HeroNode{
public int no
;
public String name
;
public String nickName
;
public HeroNode next
;
public HeroNode(int no
, String name
, String nickname
){
this.no
= no
;
this.name
= name
;
this.nickName
= nickname
;
}
@Override
public String
toString() {
return "HeroNode{" +
"no=" + no
+
", name='" + name
+ '\'' +
", nickName='" + nickName
+ '\'' +
'}';
}
}