Removing duplicates from a singly linked list
2 answers
Algorithmically this sounds impossible (or someone here will surprise me).
but you can create a simulated list without duplicates without creating an algorithm this way:
public class SimulatedNoDuplicatesList extends LinkedList {
private List originalList;
SimulatedNoDuplicatesList(List original) {
originalList = original;
}
// override all the methods that you want to use with the old list. for example
// @Override
public Object get(int i) {
if (!super.contains(i) && originalList.contains(i)) {
super.add(originalList.get(i));
}
return super.get(i);
}
}
0
source to share