https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/submissions/
题比较简单。主要是一些JAVA的api有点卡。
最终要返回一个int[]类型的数组,但给的链表的头节点,不知道长度。将链表数据装到stack中后,如何返回int[]卡住例。
toArray(T [])中,只能是Integer,但和需要不匹配,int[] 则不符合泛型。
最终只能new int,一个一个放入,没有找到简单的方法。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
import java.util.*;
class Solution {
public int[] reversePrint(ListNode head) {
ListNode p = head;
int len = 0;
while(head != null){
len ++;
head = head.next;
}
/*Deque<Integer> s = new LinkedList<Integer>();
while(head != null){
s.push(head.val);
head = head.next;
}
int len = s.size();*/
int[] a = new int[len];
for(int i=len-1;i>=0;i--){
a[i] = p.val;
p = p.next;
}
return a;// int isnot T
}
}
201

被折叠的 条评论
为什么被折叠?



