import java.util.*;
public class Solution{
public void PrintReverseList(ListNode head){
if(head==null) return;
Stack<ListNode> stack=new Stack<>();
ListNode p=head;
while(p!=null){
stack.push(p);
p=p.next;
}
while(!stack.isEmpty()){
System.out.println(stack.pop().val);
}
}
public static void main(String args[]){
ListNode h1=new ListNode(1);
ListNode h2=new ListNode(2);
ListNode h3=new ListNode(3);
h1.next=h2;
h2.next=h3;
ListNode h=null;
Solution sl=new Solution();
sl.PrintReverseList(h1);
}
}
5、从尾到头打印链表
最新推荐文章于 2022-11-01 14:28:57 发布