Quote:
Originally Posted by
tene
Always check whether N+1 node is Null before doing any logic.
Even though you know that N+1 == NULL; what you can do in the same algorithm to protect node pointer at (N - 1) from becoming a dangling pointer ?
Apart how would your function written besed on that algorithm would ever know that N == 1; and how you would protect the list-pointer pointing to the first node from becoming dangling?
A similar algorithm holds trure in binary tree node deletion; wherein the data is copied from the leaf node which is located on the branch one move either left or right of the node to be deleted logically and the actual deletion happens at the leaf node whose data is copied. This alters the data organisation but preserves the properties of the tree.
But moreover this worked there because of the deletion always happen at the leaf node and is travelled (this travel is missing in your algorithm) to be found via the entire branch and while the travel you pass through the second last node whose pointer you get and this node doesn't become a dangling one.
The TRAVEL is important.