3___ from the tail to the head print list

Subject description:

Enter a list, by returning a list sequentially from the tail to the head of ArrayList.

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        //思路:先将元素入栈,在取出到arrayList中
        Stack <Integer> s = new Stack<>();
        ArrayList<Integer> list=new ArrayList<>();
        
        // list elements stack 
        the while (listnode =! Null ) { 
            s.push (listNode.val); 
            listnode = listNode.next; 
        } 
        // stack elements in the stack 
        the while (! S.empty ()) { 
            List.add (s.pop ()); 
        } 
        return List; 
    } 
}

Stack empty judgment operations: stack.empty ();

Guess you like

Origin www.cnblogs.com/xbfchder/p/11444407.html