import java.util.Stack;
/**
* 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<>();
ListNode cur = head;
while (cur != null){
stack.push(cur.val);
cur = cur.next;
}
int[] arr = new int[stack.size()];
int i = 0 ;
while (!stack.isEmpty()){
arr[i] = stack.pop();
i ++;
}
return arr;
}
}
Leetcode-剑指Offer 06.从尾到头打印链表
最新推荐文章于 2023-07-26 09:53:50 发布