[英]What does this code do specifically? And how can I change it?
public void deleteAfter(Node prev){
prev.setNext(prev.next().next());
}
This method deletes the new node after the given prev node. 此方法删除给定prev节点之后的新节点。 Could someone explain to me what this code does specifically step by step and how I can change it so that it won't error if prev is the last node on the list. 有人可以向我解释这段代码的具体步骤,以及如何更改它,以便如果prev是列表中的最后一个节点,它将不会出错。
@param prev - The node before the place where it will be inserted. @param prev - 插入位置之前的节点。
prev.next().next()
gets the node after the next node (from the one given). prev.next().next()
获取prev.next().next()
节点之后的节点(来自给定的节点)。 That's then passed to the prev.setNext
method, making it the next node. 然后将其传递给prev.setNext
方法,使其成为下一个节点。 That essentially removes the node in between prev
and the next next node. 这基本上删除了prev
和下一个下一个节点之间的节点。
Basically, it takes prev -> next_1 -> next_2 ...
and turns it into prev -> next_2 ...
基本上,它需要prev -> next_1 -> next_2 ...
并将其转换为prev -> next_2 ...
If prev
is the last node in the list, then next()
should return null
(I assume). 如果prev
是列表中的最后一个节点,那么next()
应该返回null
(我假设)。 If that's the case, you can do a null check to avoid an error. 如果是这种情况,您可以进行空检查以避免错误。
public void deleteAfter(Node prev){
if(prev.next() != null) {
prev.setNext(prev.next().next());
}
}
The code sets a new next
such that if you had a -> b -> c
, now you'll have a -> c
. 代码设置了一个新的next
,如果你有a -> b -> c
,现在你将有a -> c
。
To avoid the error, you can check if next returns null, or maybe invoke hasNext
if such a method exists on Node. 为了避免错误,您可以检查next是否返回null,或者如果Node上存在这样的方法,则可以调用hasNext
。
if(prev.next() != null){
...
}
You have some Node, somewhere in a list. 你有一些Node,在列表中的某个地方。 I've highlighted it. 我突出了它。
... - node - node - (prev) - node - node - ...
The first call, is to set the "prev"'s next link prev.setNext(...)
but what is that next going to be set to? 第一个调用,是设置“prev”的下一个链接prev.setNext(...)
但是接下来要设置的是什么? It will be set to "next's next" or (staring with prev) 它将被设置为“下一个下一个”或(与prev一起盯着)
... - node - node - (prev) - node - node - ...
The prev.next() would be prev.next()将是
... - node - node - prev - (node) - node - ...
And the prev.next().next() would be 并且prev.next()。next()将是
... - node - node - prev - node - (node) - ...
So, after the setting operation, your arrangement would look like 因此,在设置操作之后,您的安排将如下所示
... - node - node - prev node--node - ...
\------/
Note that the prev's new "next node" was previously prev's next, next node. 请注意,prev的新“下一个节点”以前是下一个节点的下一个节点。
If you ever ask yourself "What is this code doing????", there is an excellent chance that the code can be written in a more descriptive way. 如果您曾经问过自己“这段代码是做什么的?”,那么很有可能以更具描述性的方式编写代码。 For example: 例如:
public void deleteNodeAfter(Node index) {
// Borrowed from Bill the Lizard's answer
if (index.next() != null) {
index.setNext(index.next().next());
}
}
To answer you directly: This code deletes a node from a linked list. 直接回答:此代码从链接列表中删除节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.