单向链表的反转
1.用栈实现
public void reversePrint(){
Stack<heroNode> stack = new Stack<heroNode>();
heroNode cur = head.next;
if(head.next == null){
System.out.println("空的");
return;
}
while(cur != null){
stack.push(cur);
cur = cur.next;
}
while(stack.size() > 0){
System.out.println(stack.pop());
}
}
2.头插法
public void reverse() {
heroNode cur = head.next;
heroNode newNode = new heroNode(0, "", "");
while(cur != null){
heroNode next = cur.next;
cur.next = newNode.next;
newNode.next = cur;
cur = next;
}
head.next = newNode.next;
heroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}