Java语言 从尾到头输出单链表

方法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;
}

posted @ 2019-04-16 17:24  上官蓓儿  阅读(279)  评论(0编辑  收藏  举报