除了头部或尾巴以外的节点是否可以在LinkedList中无效?

发布于 2025-02-04 10:40:21 字数 161 浏览 3 评论 0原文

我的理解是。

如果头部为空,则列表为空。 如果尾巴为零,我们已经到达列表的尽头。

所以,我的问题是, 在linkedlist abcd-null中 ABC和D可以是无效的元素吗?

随访:在Java中,链接列表以零节点结束是必不可少的吗?

My understanding is.

If the head is null, the list is empty.
If the tail is null, we have reached the end of the list.

So, my question is,
In a LinkedList a-b-c-d-null
can either a b c and d be null elements?

Follow Up: In Java, is it mandatory for a Linked List to end in a null node?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2025-02-11 10:40:21

不,因为如果一个节点为null,那么它不会存储列表中的下一个节点的信息。这意味着在null节点上没有对节点的引用收集垃圾。

No, because if a node is null then it doesn't store information about the next node in the list. This means that there are no references to nodes past the null node, and in a language like Java (seen as though you tagged this post with the java tag) they'd be garbage collected.

不知在何时 2025-02-11 10:40:21

就像其他人提到的一样,答案是否定的。您的后续问题的答案是肯定的。您可以使用以下代码自己对此进行测试

public static void main(String[] args){

LinkedList<String> ll=new LinkedList<>();
System.out.println(ll.peekFirst());}

,因为linkedlist没有元素,因此打印消息将是空的,一旦您开始将元素添加到列表中,由于缺乏更好的单词,它将将null引用推向末尾。因此,列表的结尾总是引用null。

Like others have mentioned, the answer is no. And the answer to your follow-up question is yes. You can test this out yourself using the following code

public static void main(String[] args){

LinkedList<String> ll=new LinkedList<>();
System.out.println(ll.peekFirst());}

The print message will be null, since the linkedlist has no elements, once you do start adding elements to the list, for lack of better words, it pushes the null reference towards the end. Hence the end of the list always references to null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文