Wednesday 13 August 2014

Delete the Middle Element from a Linked List and second problem Divide the List around a given value.

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;private int length;
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();
Node md = l1.head;
for(int i=0;i<l1.length/2-1;i++){md=md.getNext();}
l1.deleteMiddle(md);System.out.println();
l1.printList();
l1.divideListAround(13);System.out.println();
l1.printList();

}


private Node createNode(int data){
Node newNode = new Node();newNode.setData(data);newNode.setNext(null);return newNode;
}


private void add(int data){ 
length++;
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();}
}

// We assume that reference to the node is given:O(1) time to do that.
private void deleteMiddle(Node mid){
length--;
mid.setData(mid.getNext().getData());
mid.setNext(mid.getNext().getNext());
}


private void divideListAround(int val){
LL left = new LL();
LL right = new LL();
Node h = head;
while(h!=null){
if(h.getData()<val)
left.add(h.getData());
else
right.add(h.getData());
h=h.getNext();
}
//Merge the lists
Node headLeft = left.head;
while(headLeft.getNext()!=null)
{
headLeft=headLeft.getNext();
}
headLeft.setNext(right.head);
this.head=left.head;
}

}

Tuesday 12 August 2014

Remove duplicates from an Unsorted Linked List

//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();
}
}
}