剑指offer--从尾到头打印链表--3

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
思路: 桟

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
	public ArrayList<Integer> printListFromTailToHead(ListNode listNode) 		{
    Stack<Integer> stack = new Stack<>();
    ArrayList<Integer> result = new ArrayList<>();
    while(listNode!=null){
        stack.push(listNode.val);
        listNode = listNode.next;
    }
    while(!stack.isEmpty()){
        result.add(stack.pop());
    }
    return result;
}
}

猜你喜欢

转载自blog.csdn.net/weixin_44017425/article/details/105276192