力扣:剑指 Offer 06. 从尾到头打印链表

This commit is contained in:
huangge1199 2021-02-07 14:08:06 +08:00
parent 3380b0b8a9
commit 56c6c43645

View File

@ -0,0 +1,25 @@
package com.code.leet.study.t20210207;
import com.code.leet.entiy.ListNode;
import java.util.ArrayList;
import java.util.List;
/**
* 输入一个链表的头节点从尾到头反过来返回每个节点的值用数组返回
*/
public class ReversePrint {
public int[] reversePrint(ListNode head) {
List<Integer> list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
int size = list.size();
int[] arr = new int[size];
for (int i = size - 1; i >= 0; i--) {
arr[i] = list.get(size - 1 - i);
}
return arr;
}
}