这个也是在idea运行结果正确,但是牛客网都无法编译!!!
import java.util.ArrayList;
import java.util.Scanner;
//输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
class ListNode{
int val;
ListNode next = null;
public int getVal () {
return val;
}
public void setVal (int val) {
this.val = val;
}
public ListNode getNext () {
return next;
}
public void setNext (ListNode next) {
this.next = next;
}
ListNode head = null;
int size = 0;
public void add(int d){
ListNode k = new ListNode ();
k.setVal ( d );
if (head == null){
head = k;
}
else {
ListNode ss = head;
for (int i=0; i<size-1; i++)
{
ss = ss.getNext ();
}
ss.setNext ( k );
}
size++;
}
}
public class Solution11 {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> aa = new ArrayList<Integer>( );
ArrayList<Integer> bb = new ArrayList <Integer> ( );
ListNode ff = listNode.head;
while (ff != null ){
aa.add ( ff.val );
ff = ff.next;
}
for (int i=aa.size ()-1;i>=0;i--){
bb.add ( aa.get ( i ) );
}
return bb;
}
public static void main (String[] args) {
Solution11 q = new Solution11 ();
ListNode tt = new ListNode ();
Scanner sc = new Scanner ( System.in );
System.out.println ("输入n个数");
int n =sc.nextInt ();
int[] num = new int[n];
for (int i=0; i<num.length; i++){
num[i] = sc.nextInt ();
}
for (int i=0; i<num.length; i++){
tt.add ( num[i] );
}
ArrayList<Integer> res ;
res = q.printListFromTailToHead ( tt );
System.out.println (res);
}
}