此题有两种思路,思路一:先反转链表,然后存入新建的数组中。
思路二:直接将链表存入新建的数组中,然后将数组中的数据原地置换。
class Solution {
public int[] reversePrint(ListNode head) {
ListNode cur = head;
ListNode pre = null;
ListNode temp = cur;
int len = 0;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
++len;
}
int [] ans = new int [len];
int cnt = 0;
while(pre != null){
ans[cnt++] = pre.val;
pre = pre.next;
}
return ans;
}
}