I am trying to remove a minimum element from a Java LinkedList.
In order to find the minimum, I have to traverse through the LinkedList once. I would like to save the Node or Iterator of that element to remove it in O(1).
The normal
list.remove(Object o)
takes O(n) steps.
void removeMin(LinkedList<Integer> list) {
ListIterator<Integer> itList = list.listIterator();
Integer min = itList.next();
while(itList.hasNext()) {
Integer curVal = itList.next();
if(curVal < min) {
min = curVal
// Copy the iterator here?
}
}
// remove min from list here.
}
Is there a way, to copy the iterator, when a new minimum is found, so I can call the remove() of the iterator afterwards?