1. 程式人生 > >Java語言 從尾到頭輸出單鏈表

Java語言 從尾到頭輸出單鏈表

push rev tac pri empty null sta cnblogs blog

方法1:https://www.cnblogs.com/sgbe/p/10717861.html

方法2:用棧

public static Node1 printRevers(Node1 head) {
if(head==null||head.next==null) return head;
Stack<Character> st=new Stack();
Node1 dummy=head;
while(dummy!=null) {
st.push(dummy.val);
dummy=dummy.next;
}
Node1 head1=new Node1(‘0‘);
Node1 h=head1;

while(!st.isEmpty()) {
Node1 temp=new Node1(st.pop());
h.next=temp;
h=h.next;
}
return head1.next;
}

Java語言 從尾到頭輸出單鏈表