I am writing some code where I need to remove an item in a circularly linked list (who's head acts as a dummy node) and return it (if removing the first node). I think I have the code just about right, but I'm not sure.
Am I understanding this correctly? (starting with a dummy node)
dummy -> A -> B -> C -> D -> dummy (wrap around to the dummy node)
So if I want to remove the first actual piece of data(A), I would need to assign it to a temp variable. So Node first = head.next. Then I need to have the dummy head reference "b" so I would need to do head.next = first.next. Is this all that needs to be done?
private Node remove()
{
Node returnNode = head.next;
head.next = returnNode.next;
return returnNode;
}
In the case of removing any node N from the list (assuming it is on the list), it is sort of the same concept? So from the example above, lets say we want to remove node B. In this case, we need to set B.next = B.previous and B.previous = B.next correct? Or do I need to do something like B.previous.next = B.next and B.next.previous = B.previous? Do I need to traverse the list to find the element to remove?
private void removeNode(Node n)
{
n.next = n.previous; // or n.previous.next = n.next
n.previous = n.next; // or n.next.previous = n.previous
}