Reverse Print 2021-06-24 05:20

Problem Description

public int[] reversePrint(ListNode head) {
    ListNode p = head;
    int count = 0;
    while (p != null) {
        p = p.next;
        count++;
    }
    int[] res = new int[count];
    int index = res.length - 1;
    while (head != null) {
        res[index] = head.val;
        head = head.next;
        index--;
    }
    return res;
}
Runtime Memory
0 ms 39 MB

henryxi leetcode list

EOF