For the exam, I train to write the some algorithms to the final exam. One of them is creating the reverse() method, which removes doubles in the List<> . The problem that the void method only removes one double or two. How can I change the method?
Here, the code of the List.java file. The methods removeFromBack(), removeFromFront(), insertAtBack(), insertAtFront(), print(), isEmpty(), the classes List and ListNode are already defined in Deitel's Java book. Additionally, all imports are done:
public void removeDuplicates() {
ArrayList<T> toCheck = new ArrayList<T>();
ListNode<T> current = firstNode;
while (current != null) {
toCheck.add(current.data);
current = current.nextNode;
}
current = firstNode;
HashSet<T> toCheck2 = new LinkedHashSet<T>();
for (T element: toCheck) {
toCheck2.add(element);
}
for (T element: toCheck2) {
removeFromBack();
insertAtBack(element);
}
}