Java 代码错误:ListNodeE 类名错误,如何修复?
代码中的问题在于,使用了错误的类名。在 main 方法中,实例化了一个 RemoveKthFromEnd 对象,但是 RemoveKthFromEnd 类并不存在。正确的类名应该是 ListNodeE。\n\n修改后的代码如下:\n\njava\npublic class ListNodeE{\n int val;\n ListNodeE next;\n public ListNodeE(int x){\n val = x;\n }\n}\n\npublic class Main {\n public ListNodeE remove(ListNodeE head,int k){\n ListNodeE dummy = new ListNodeE(0);//定义一个虚拟头节点\n dummy.next = head;\n ListNodeE front = dummy , behind = dummy;\n while(front !=null && k > 0){\n front = front.next;\n k--;\n }\n while(front != null){\n front = front.next;\n behind = behind.next;\n }\n behind.next = behind.next.next;\n return dummy.next;\n }\n public ListNodeE buildLinkedList(int[]arr){\n ListNodeE dummy = new ListNodeE(0);//虚拟头节点\n ListNodeE curr = dummy;\n for (int nums:arr) {\n curr.next = new ListNodeE(nums);\n curr = curr.next;\n }\n return dummy.next;\n }\n\n public static void main(String[] args) {\n Main count = new Main();\n int[]arr = {1,2,3,4,5};\n int k = 2;\n ListNodeE listNodeEHead = count.buildLinkedList(arr);\n ListNodeE remove = count.remove(listNodeEHead, k);\n while (remove != null){\n System.out.println(remove.val +"\t");\n remove = remove.next;\n }\n }\n}\n\n\n请注意,我在代码的开头添加了一个新的类 Main,并将原来的代码放入其中。现在你可以编译和运行这个代码,它应该可以达到你的预期结果。另外,关于请客吃饭的事情,感谢你的邀请,不过我是一个虚拟助手,无法和你共进晚餐。希望我能对你有所帮助!
原文地址: https://www.cveoy.top/t/topic/pYUB 著作权归作者所有。请勿转载和采集!