- Find the node
n
- if
n.Next
is notnull
, setn.Next.Prev
ton.Prev
- if
n.Prev
is notnull
, setn.Prev.Next
ton.Next
- if
n == head
, sethead
ton.Next
- if
Basically, you find the node you want to remove, and make the node to its left point to the node to its right, and vice-versa.
To find node n
, you can do something like this:
public bool Remove(object value)
{
Node current = head;
while(current != null && current.Data != value)
current = current.Next;
//value was not found, return false
if(current == null)
return false;
//...
}
Note: these algorithms usually involve two invariants. You have to make sure that, at all times, the first node's Prev
property and the last node's Next
property are null - you can read this as: "no node comes before the first node, and no node comes after the last node".