//Author: Santanu Naskar//
// Here I have discussed both the methods of removing duplicates: Hash Table approach, Non-buffered //approach......
class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class LL {
private Node head;
public static void main(String[] args) {
LL l1 = new LL();
l1.add(12);l1.add(20);l1.add(6);l1.add(12);l1.add(20);l1.add(6);
l1.printList();System.out.println();
l1.removeDuplicatesWithoutUsingBuffer_O_n2();System.out.println();
l1.printList();System.out.println();
}
private Node createNode(int data){
Node newNode = new Node();newNode.setData(data);newNode.setNext(null);return newNode;
}
private void add(int data){
Node nn = createNode(data);
if(this.head==null){ this.head=nn; }
else
{
Node temp = this.head;
while(temp.getNext()!=null)temp=temp.getNext();
temp.setNext(nn);
}
}
private void printList(){
Node head = this.head;
while(head!=null){System.out.print(head.getData()+"->");head=head.getNext();}
}
// Function to remove duplicates from an unsoretd LL //
private void removeDuplicatesUsingHashTable_O_n(){
java.util.Hashtable<Integer,Integer> ht = new java.util.Hashtable<Integer,Integer>();
Node current = this.head, previous = null; //current pointing to head and previous set to null
while(current!=null)
{
if(ht.containsKey(current.getData())) // If hashtable doesn't already contain the current.data we need to remove that from the list.
{previous.setNext(current.getNext());} // So we set the previous pointer to current's next in order to skip the duplicate value.
else
{
ht.put(current.getData(), 1); // Put the non-occured value into the hash table for future checking
previous=current; // Set previous to node current is pointing now.
}
current=current.getNext();
}
}
private void removeDuplicatesWithoutUsingBuffer_O_n2(){
Node current = head;
while(current!=null){
Node runner = current;
while(runner.getNext()!=null){ // Its upto you to think why i have not used runner!=null in place of runner.getNext()!=null
if(runner.getNext().getData()==current.getData())
runner.setNext(runner.getNext().getNext());
else
runner=runner.getNext();
}
current=current.getNext();
}
}
}
// Here I have discussed both the methods of removing duplicates: Hash Table approach, Non-buffered //approach......
class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class LL {
private Node head;
public static void main(String[] args) {
LL l1 = new LL();
l1.add(12);l1.add(20);l1.add(6);l1.add(12);l1.add(20);l1.add(6);
l1.printList();System.out.println();
l1.removeDuplicatesWithoutUsingBuffer_O_n2();System.out.println();
l1.printList();System.out.println();
}
private Node createNode(int data){
Node newNode = new Node();newNode.setData(data);newNode.setNext(null);return newNode;
}
private void add(int data){
Node nn = createNode(data);
if(this.head==null){ this.head=nn; }
else
{
Node temp = this.head;
while(temp.getNext()!=null)temp=temp.getNext();
temp.setNext(nn);
}
}
private void printList(){
Node head = this.head;
while(head!=null){System.out.print(head.getData()+"->");head=head.getNext();}
}
// Function to remove duplicates from an unsoretd LL //
private void removeDuplicatesUsingHashTable_O_n(){
java.util.Hashtable<Integer,Integer> ht = new java.util.Hashtable<Integer,Integer>();
Node current = this.head, previous = null; //current pointing to head and previous set to null
while(current!=null)
{
if(ht.containsKey(current.getData())) // If hashtable doesn't already contain the current.data we need to remove that from the list.
{previous.setNext(current.getNext());} // So we set the previous pointer to current's next in order to skip the duplicate value.
else
{
ht.put(current.getData(), 1); // Put the non-occured value into the hash table for future checking
previous=current; // Set previous to node current is pointing now.
}
current=current.getNext();
}
}
private void removeDuplicatesWithoutUsingBuffer_O_n2(){
Node current = head;
while(current!=null){
Node runner = current;
while(runner.getNext()!=null){ // Its upto you to think why i have not used runner!=null in place of runner.getNext()!=null
if(runner.getNext().getData()==current.getData())
runner.setNext(runner.getNext().getNext());
else
runner=runner.getNext();
}
current=current.getNext();
}
}
}
No comments:
Post a Comment