链表:如何删除最后一个节点,在单个链表中我们有指向最后一个节点的指针 [英] linked list : how to delete last node, where we have pointer to last node in a single linked list
问题描述
在单个链接列表中,我们可以删除带有指向其的指针的节点。
这可以通过将下一个节点数据复制到当前节点直到到达终点来实现。
In a single linked list, we can delete a node with holding pointer to it. This can be achieved by copying next node data into current node until we reach end.
但是,我们如何通过持有并知道指向该节点的指针来删除最后一个节点self?
But, How can we delete last node by holding and knowing pointer to self ?
-谢谢
推荐答案
这通常是不可能的。您需要最后一个节点之前的节点地址。该节点的 next
指针应设置为 NULL
。无法从单链列表中的最后一个节点直接访问最后一个节点。
This is generally not possible. You need the address of the node before the last node. This node should have it's next
pointer set to NULL
. The node before last cannot be directly accessed from the last node in a singly linked list.
但是,如果您可以访问 head , code>节点,您可以再次遍历列表以使该节点在最后一个节点之前。
However, if you have access to the head
node, you can traverse the list again to get the node before the last node.
这篇关于链表:如何删除最后一个节点,在单个链表中我们有指向最后一个节点的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!