力扣:剑指 Offer 06. 从尾到头打印链表
This commit is contained in:
parent
3380b0b8a9
commit
56c6c43645
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user