英文:
What type of datatype is ListNode in java?
问题
I have encountered this datatype in Java in a problem in Leetcode called ListNode and have looked up to see what ListNode does but can't find anything on it. Does anyone know what this datatype does as I'm new to Java and don't understand what it does and neither can I find any information on it.
here is the code to the problem I need to solve on Leetcode:
/**
* 单链表的定义。
*/
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
}
}
英文:
I have encountered this datatype in java in a problem in Leetcode called ListNode and have looked up to see what ListNode does but can't find anything on it. Does anyone know what this datatype does as I'm new to Java and don't understand what it does and neither can I find any information on it.
here is the code to the problem I need to solve on leetcode:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
}
}
答案1
得分: 1
以下是您要翻译的内容:
"... Does anyone know what this datatype does ..."
这是一个自定义的数据容器;在Java中称为一个_对象。
在Java中,任何类结构都会被视为一个_对象。
具体来说,这种数据结构被称为_链表_。
下面是一个包含文本值的基本_对象_的示例。class ABC { String string = "stack overflow"; }
public static void main(String[] args) { ABC abc = new ABC(); System.out.println(abc.string); }
输出
stack overflow
这是_ListNode_ _对象_的示例用法。
ListNode listNode = new ListNode(1); listNode.next = new ListNode(2); System.out.println(listNode.val); System.out.println(listNode.next.val);
输出
1 2
英文:
> "... Does anyone know what this datatype does ..."
It is a custom data container; referred to as an object, in Java.
Any class structure in Java is going to be considered an object.
Although, more specifically, that data structure is called a linked list.
Here is an example of a basic object, which will contain a text value.
class ABC {
String string = "stack overflow";
}
public static void main(String[] args) {
ABC abc = new ABC();
System.out.println(abc.string);
}
Output
stack overflow
And, here is an example usage of the ListNode object.
ListNode listNode = new ListNode(1);
listNode.next = new ListNode(2);
System.out.println(listNode.val);
System.out.println(listNode.next.val);
Output
1
2
答案2
得分: 0
这是LeetCode专门为这个问题定义的特定/自定义类。它在JDK中不存在。类的定义在类上面的文档中。
您可以使用该类将代码提交到LeetCode,假装它存在并已导入。如果您想在本地编译代码,除了从描述中复制粘贴代码并放入项目中外,没有其他办法。
英文:
That's a specific/custom class defined by LeetCode specially for the problem. It doesn't present in the JDK. The definition of class is in the doc above the class.
You can submit code to the LeetCode using that class pretending it exists and already imported. If you want to compile your code locally, there is no other way than copy-paste it from the description and put somwhere in your project.
答案3
得分: 0
新手学Java。
我也遇到了你的问题。我看到一个同事写的类。我以为它是随着JDK一起提供的,所以我在API文档中搜索了很长时间,但我找不到它。
后来,我知道我们可以根据自己的需求定义一个类,并使用这个类来执行一些特定的操作。
这就像厨房里的调味料,总是不够多样化。
有时候你需要种一些迷迭香来增强菜肴的味道。
如果将来我们需要自己编写一些类,我们如何让同事知道它的功能?
我们可以添加必要的注释,告诉未来的用户这个类是用来做什么的,就像你提供的代码一样:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
英文:
new to Java.
I have also encountered your problem. I came across a class written by a colleague. I thought it came with jdk, so I searched for a long time in the api documentation, but I couldn't find it.
Later, I know that we can define a class ourselves according to our needs, and use this class to do some specific things.
It's like seasoning in the kitchen, there is always not enough variety.
Sometimes you have to grow some rosemary to enhance the taste of dishes.
If we need to write some classes ourselves in the future, how can we let our colleagues know its functions?
We can add the necessary comments to tell future users what this class is for, just like the code you provided:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论