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.
But, How can we delete last node by holding and knowing pointer to self ?
-Thanks
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.
However, if you have access to the head
node, you can traverse the list again to get the node before the last node.
when we delete the node, it means we free the memory pointed by the pointer not the pointer itself. Hence, Same can be done in last node case.
ie ptr=addressof(last node),which is not equal to address of the pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.