好奇怪,在idea上面编译运行没问题!!!但是牛客网上面过不了!!有朋友提醒一下为啥在牛客网编译会出现这样的提示么,已经出现两次,但是还是手足无措,求指教,万分感谢:
idea:
牛客网:
import java.util.Scanner;
//输入一个链表,输出该链表中倒数第k个结点
public class Solution8 {
private class ListNode{
private int data;
private ListNode next;
public int getData () {
return data;
}
public void setData (int data) {
this.data = data;
}
public ListNode getNext () {
return next;
}
public void setNext (ListNode next) {
this.next = next;
}
}
private ListNode head = null;
private int size = 0;
public int getSize () {
return size;
}
//增加一个结点
public void add(int d){
ListNode a = new ListNode();
a.setData(d);
if (head == null)
{
head = a;
}
else {
ListNode temp = head;
for (int i = 0; i < size-1 ; i++)
{
temp = temp.getNext();
}
temp.setNext(a);
}
size++;
}
public ListNode FindKthToTail(ListNode head, int k) {
if (k<=0) return null;
ListNode p1 = head;
ListNode p2 = head;
for (int i = 0; i < k-1; i++){
//p2比p1多走k步
if (p2==null)return null;
p2 = p2.next;
}
if (p2 == null) return null;
while(p2.next != null){
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
public static void main (String[] args) {
Solution8 ss = new Solution8 ();
Scanner sc = new Scanner ( System.in );
System.out.println ("设置数组长度:");
int n = sc.nextInt ();
int[] a = new int[n];
System.out.println ("向链表中放入值:");
for (int i=0; i<a.length; i++){
a[i] = sc.nextInt ();
}
for (int i = 0; i<a.length; i++){
ss.add ( a[i] );
}
System.out.println ("设置倒数第k个值:");
int k = sc.nextInt ();
System.out.println ("输出第k个值:");
ListNode listNode = ss.FindKthToTail ( ss.head,k );
int d = listNode.data;
System.out.println (d);
}
}