leetcode-面试题6-从尾到头打印链表

 题目描述:

 

 java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        while(head != null){
            stack.push(head.val);
            head = head.next;
        }
        int size = stack.size();
        int [] array = new int[size];
        for(int i = 0; i < size; i++){
            array[i] = stack.pop();
        }
        return array;

    }
}

 

posted @ 2020-04-02 17:06  oldby  阅读(114)  评论(0编辑  收藏  举报