链表实际上是如何改变的
我想知道是否有人可以帮助我解决这个问题。我相信我大部分都理解代码和逻辑。我可以跟踪代码,这是有道理的,但我不明白的一件事是......前面的 LinkedListNode 实际上如何更改传入的 LinkedListNode n ?
在我看来,该函数循环遍历 n,如果尚未找到该元素,则将其放入哈希表中。但是当再次找到它时,它使用这个新创建的 LinkedListNode previous 来跳过重复项并链接到后面的元素,即 n.next。 这实际上是如何断开 LinkedListNode n 的呢?看起来 previous 是没有重复项的 LinkedListNode,但由于此函数中没有返回任何内容,因此 n 必须是发生更改的那个。我想我没有看到 n 实际上是如何改变的。
清晰彻底的帮助将不胜感激。谢谢 = )
public static void deleteDups(LinkedListNode n){
Hashtable table = new Hashtable();
LinkedListNode previous = null;
while(n != null){
if(table.containsKey(n.data))
previous.next = n.next
else{
table.put(n.data, true);
previous = n;
}
n = n.next;
}
}
这行不是
LinkedListNode previous = null;
创建一个新的 LinkedListNode 吗?
所以这就是我的逻辑... 假设参数 n 作为
5 -> 传入6-> 5-> 7
当代码第一次运行时,previous为null。它进入 else 语句,而 previous 现在是 5?然后 n = n.next 行使 n 为 6?现在哈希表有 5,它再次循环并进入 else。 prev 现在是 6,hastable 有 6。然后 n 变成 5。它再次循环,但这次它进入 if,prev 现在是 7。n 将变成 7。我看到 prev 跳过了 5,但是.. .prev 如何取消链接 n?看起来 prev 是不包含重复项的 LinkedListNode
i was wondering if anyone could help me with this question. i believe i understand the code and logic for the most part. i can trace code and it makes sense, but one thing i don't get is....how does the LinkedListNode previous actually change the LinkedListNode n that is passed in?
it seems to me that the function loops through n, and if the element is not yet found puts it into a hashtable. but when it is found again, it uses this newly created LinkedListNode previous to skip over the duplicate and link to the following element, which is n.next.
How does that actually disconnect LinkedListNode n? It seems like previous would be the LinkedListNode that has no duplicates, but since nothing gets returned in this function, n must be the one that changes. I guess I'm not seeing how n actually gets changed.
Clear and thorough help would be much appreciated. Thank you = )
public static void deleteDups(LinkedListNode n){
Hashtable table = new Hashtable();
LinkedListNode previous = null;
while(n != null){
if(table.containsKey(n.data))
previous.next = n.next
else{
table.put(n.data, true);
previous = n;
}
n = n.next;
}
}
doesn't the line...
LinkedListNode previous = null;
create a new LinkedListNode?
so this is my logic of how i'm doing it...
lets say the argument, n, gets passed in as
5 -> 6 -> 5 -> 7
when the code first runs, previous is null. it goes into the else statement, and previous is now 5? and then the line n = n.next makes n 6? now the hashtable has 5, and it loops again and goes into the else. prev is now 6 and the hastable has 6. then n becomes 5. it loops again but this time it goes into the if, and prev is now 7. and n will become 7. i see that prev skipped over 5, but ...how is prev unlinking n? it seems like prev is the LinkedListNode that contains no duplicates
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看线
该行导致传递的节点 n 发生变化 - 在每次迭代中有效地将其向前移动一个节点。
不,这里没有新创建节点。节点
previous
总是指向现有LinkedList中的一个节点,其中传递的节点n是该节点之一。 (可能是起始节点)。是什么让您认为它是新创建的?看起来您对节点和引用(以及整个 LinkedList)如何在 Java 中工作的理解感到困惑。因为所有修改都发生在 Node 的数据上,而不是引用本身(呃..这并不完全正确),所以传递给该方法的原始 LinkedList 在该方法返回后确实会被修改。您需要详细分析 LinkedList 结构和工作原理以了解其工作原理。我建议首先弄清楚 Java 中的值传递和引用传递是什么。
编辑:
您对运行的分析是正确的,但是您的困惑仍然存在,因为您在某些事情上概念上不清楚。
在分析结束时,您会问“..
prev
如何取消链接n
?似乎prev
是LinkedListNode< /code> 不包含重复项“
这是一团糟 - 首先,您需要区分
LinkedListNode
和LinkedList
本身。prev
和n
是LinkedListNode
的两个实例,而不是LinkedList
本身。在您的示例中,LinkedList
未命名(我们没有一个名称来引用它)。这是原始列表 - 没有其他列表。其次,在你的插图中,你显示的数字只是节点的一部分,称为节点数据。您错过的另一部分是每个节点中隐含的
next
LinkedListNode 引用。您绘制的链接 ->实际上是每个节点中的下一个引用。当您说 prev 跳过 5 时,实际发生的情况是数据为 6 的节点的下一个指向数据为 7 的节点。在开始时:
5|下一个 -> 6|下一个 -> 5|下一个 -> 7|next->NULL
跳过 5 后:
5|下一个 -> 6|下一个 -> 7|next->NULL
如您所见,链表已更改!无论是使用 prev 还是 n 进行更改,更改都会保留在列表中。
Look at the line
This line causes the passed node n to change - effectively moving it one node forward with each iteration.
No, no node is newly created here. The node
previous
always points to a node in the existing LinkedList of which the passed node n is one of the nodes. ( may be the starting node ). What makes you think it is newly created ?It looks like you are confused in your understanding of how nodes and references ( and so a LinkedList as a whole ) works in Java. Because all modifications occur on the Node's data , and not the reference itself , ( ugh.. thats not entirely correct ), the original LinkedList passed to the method does get modified indeed after the method returns. You will need to analyse the LinkedList structure and workings in details to understand how this works. I suggest first get clarity about what pass by value and pass by references are in Java.
edit :
Your analysis of the run is correct , however your confusion still remains because you are not conceptually clear on certain things.
Towards the end of your analysis , you ask "..how is
prev
unlinkingn
? it seems likeprev
is theLinkedListNode
that contains no duplicates "This is a mess - first , you need to differentiate between a
LinkedListNode
and aLinkedList
itself.prev
andn
are two instances ofLinkedListNode
, notLinkedList
itself. In your example , theLinkedList
is unnamed ( we dont have a name to refer to it ). This is the original list - there is no other list.Second , in your illustration , the numbers you show are only one part of the node , called the node data. The other part, that you have missed out, is the
next
LinkedListNode reference that is implicit in every node. The link that you draw -> is actually the next reference in each node.When you say that prev skips 5 , what actually happens is that the next of node with data 6 is made to point to node with data 7.At start :
5|next -> 6|next -> 5|next -> 7|next->NULL
After 5 is skipped :
5|next -> 6|next -> 7|next->NULL
As you see , the linkedlist is changed ! It does not matter if it was changed using prev or n, the change remains in the list.
是进行删除的部分。它将前一个节点的
next
字段的引用分配给当前节点之后的节点,实际上取消了当前节点的链接。Is the section which does the deletion. It assigns the reference of the prior node's
next
field to the node after the current node, in effect unlinking the current node.