/**
- Definition for singly-linked list.
- public class ListNode {
- int val;
- ListNode next;
- ListNode(int x) { val = x; }
} */ class Solution { public int[] reversePrint(ListNode head) {
//先跑一遍链表的长度//然后根据长度申请数组空间//然后按顺序倒叙存储//计数器ListNode tempnode = head;int count = 0;while(tempnode != null){tempnode = tempnode.next;//计数加一count++;}int[] num = new int[count];tempnode = head;for(int i = count-1;i>=0;i--){num[i] = tempnode.val;tempnode = tempnode.next;}return num;} }
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:39.3 MB, 在所有 Java 提交中击败了24.14%的用户
