学习了单链表的一些简单操作,打卡插眼
//链表反转 接受头节点,然后对整个单链表进行反转,破坏链表结构
public void reversetList(HeroNode head){
if(head.next ==null || head.next.next == null)
return;
//定义辅助头节点:reversehead
//定义辅助指针:cur, next
HeroNode reversehead = new HeroNode(0,"","");
HeroNode cur = head.next;
HeroNode next = null; //储存cur节点的下一个节点
while(cur != null){
next = cur.next;
cur.next = reversehead.next;
reversehead.next = cur;
cur = next;
}
//将头节点指向已经反转的链表
head.next = reversehead.next;
}
//实现对单链表的逆序打印,且不破坏链表结构
public void resersePoint(HeroNode head){
if(head.next == null)
return;
//cur: 辅助变量 stack:栈
HeroNode cur = head.next;
Stack<HeroNode> stack = new Stack<HeroNode>();
//压入栈
while(cur != null){
stack.push(cur);
cur = cur.next;
}
//出栈
while(stack.size() > 0){
System.out.println(stack.pop());
}
}