The Linked List program is available here: link
public void RemoveDuplicatesWithBuffer() {
LinkedList L = this;
LinkedList previous = null;
HashSet<Integer> H = new HashSet<>();
while(L!=null) {
if(H.contains(L.data)) {
previous.next = L.next;
} else {
H.add(L.data);
previous=L;
}
L=L.next;
}
}
public void RemoveDuplicatesWithoutBuffer() {
LinkedList L = this;
while(L!=null) {
LinkedList runner = L;
while(runner.next!=null) {
if(runner.next.data==L.data)
runner.next=runner.next.next;
else
runner=runner.next;
}
L=L.next;
}
}
No comments:
Post a Comment