The closest thing you can get to a direct reference is an Iterator
that points to a specific point.
If you call remove()
on such an Iterator
it should work in O(1):
LinkedList<Object> linkedList = ...;
Iterator<Object> it = linkedList.iterator();
while (it.hasNext()) {
if (matchesSomeCondition(it.next()) {
it.remove();
}
}
Note that this sample code definitely doesn't run in O(1), it's specifically just the remove()
call that can have that efficiency. If you haven't already identified the node in some way (such as positioning an Iterator
at that place), then you won't be able to remove an element from a LinkedList
in O(1) time.
Edit and since you mention "last recently added" element then maybe a ListIterator
is the thing to use, since it has an add()
method. If you can efficiently implement all your adding/removing using a ListIterator
, then you can keep the traversal operations over the LinkedList
to a minimum. In fact, if you always use indexes to add/remove objects from your LinkedList
then you loose a lot of its efficiency (since each add
/remove
call has to find the affected item first via traversal).