除了头部或尾巴以外的节点是否可以在LinkedList中无效?
我的理解是。
如果头部为空,则列表为空。 如果尾巴为零,我们已经到达列表的尽头。
所以,我的问题是, 在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,因为如果一个节点为
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 thenull
node, and in a language like Java (seen as though you tagged this post with thejava
tag) they'd be garbage collected.就像其他人提到的一样,答案是否定的。您的后续问题的答案是肯定的。您可以使用以下代码自己对此进行测试
,因为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
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.