I'm thinking of doing the following:
for(LinkedListNode<MyClass> it = myCollection.First; it != null; it = it.Next)
{
if(it.Value.removalCondition == true)
it.Value = null;
}
What I'm wondering is: if simply pointing the it.Value
to null actually gets rid of it.
The LinkedList. iterator() method returns an iterator over the sequence of elements of the Linked List in proper order.
An Iterator can be used to loop through an LinkedList. The method hasNext( ) returns true if there are more elements in LinkedList and false otherwise. The method next( ) returns the next element in the LinkedList and throws the exception NoSuchElementException if there is no next element.
It depends on what you're trying to accomplish. If you want to search or access a specific element, arrays are faster, but if you want to insert or delete an element, a linked list is faster.
You can make use of Floyd's cycle-finding algorithm, also known as tortoise and hare algorithm. The idea is to have two references to the list and move them at different speeds. Move one forward by 1 node and the other by 2 nodes. If the linked list has a loop they will definitely meet.
Setting the it.Value to null will not remove the node from the list Here is one way:
for(LinkedListNode<MyClass> it = myCollection.First; it != null; )
{
LinkedListNode<MyClass> next = it.Next;
if(it.Value.removalCondition == true)
myCollection.Remove(it); // as a side effect it.Next == null
it = next;
}
Surely (with a linked list) you need to change the link.
Eg, if you want to remove B from the LL A-B-C, you need to change A's link to B to C.
I'll admit I'm not familiar with the .NET implementation of linked lists but hopefully that's a start for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With