Allow to change original data
class Solution {
public int[] reversePrint(ListNode head) {
if (head == null) return new int[0];
ListNode dummy = new ListNode(-1);
ListNode p1 = dummy, p2 = head;
int count = 0;
while (p2 != null) {
ListNode tmp = p2.next;
p2.next = p1.next;
p1.next = p2;
p2 = tmp;
count++;
}
int[] num = new int[count];
p1 = dummy.next;
count = 0;
while (p1 != null) {
num[count++] = p1.val;
p1 = p1.next;
}
return num;
}
}
Not allow to change
num
end and list’s firstNot allow to change
Not allow to change