學習了單鏈表的一些簡單操作,打卡插眼
//鏈表反轉 接受頭節點,然後對整個單鏈表進行反轉,破壞鏈表結構
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());
}
}