Showing posts with label Linked List. Show all posts
Showing posts with label Linked List. Show all posts

Friday, 17 June 2016

Merge two sorted Linked List

 Merge 2 Sorted LinkedList :

class MergeLinkedList
{
  static class Node
  {
    int data;
    Node next;
  }
  public static void main(String[] args) {
    Node head1 = new Node();
    Node head2=  new Node();
    Node head3=  new Node();
    head3=null;
    head1.data=0;
    head2.data=1;
    head1=insert(insert(insert(insert(insert(insert(head1,2),5),7),8),9),11);
    head2=insert(insert(insert(insert(insert(head2,2),6),8),11),13);
    while(true)
    {
      if(head1==null && head2==null)
      {
        break;
      }
      else if(head1==null)
      {
        head3=insert(head3,head2.data);
        head2=head2.next;
      }
      else if(head2==null)
      {
        head3=insert(head3,head1.data);
        head1=head1.next;
      }
      else if(head1.data<=head2.data)
      {
        head3=insert(head3,head1.data);
          head1=head1.next;
      }
      else{
        head3=insert(head3,head2.data);
          head2=head2.next;
      }
    }
    printList(head3);
  }

  public static Node insert(Node head, int data) {
   Node node = new Node();
   node.data = data;
   node.next = null;
      if(head==null)
      return node;
 Node currentNode= head;
 while(currentNode.next!=null)
 {
     currentNode=currentNode.next;
     }

   currentNode.next=node;
   return head;
  }

 /**
  * Print All Elements of Linked List
  *
  * @param      node  The node
  */
  public static void printList(Node node) {
   Node currentNode = node;
   while (currentNode != null) {
    System.out.println(currentNode.data);
    currentNode = currentNode.next;
   }

}
}

Thursday, 16 June 2016

Delete Nth Node From Linked List

Delete Nth Node  From Linked List : 

class DeleteNthNode
{
  static class Node {
   int data;
   Node next;
  }
  /**
  @main Method
  **/
  public static void main(String[] args) {
  Node head = insert(insert(insert(insert(null, 1), 2), 3),4);
  head= delete(head,0);
  head= delete(head,2);
  printList(head);

  }
  /**
   * Delete Node At Nth Position of Linked List
   *
   * @param      head  The head
   *
   * @param      Nth Position
   * @return     return head of Linked List
   */
  public static Node delete(Node head,int pos) {
   if (head == null) {
    return head;
   }
   if(pos==0)
   {
     Node next = head.next;
     return next;
   }
   else{
     Node currentNode=head;
     while((pos-1)>0)
     {
       currentNode=currentNode.next;
       pos--;
     }
    Node next = currentNode.next.next;
    currentNode.next=next;
    return head;
   }

  }

  /**
   * Insert Node at End
   *
   * @param      head  The head node
   * @param      data  The data
   *
   * @return     { description_of_the_return_value }
   */
   public static Node insert(Node head, int data) {
    Node node = new Node();
    node.data = data;
    node.next = null;
       if(head==null)
       return node;
  Node currentNode= head;
  while(currentNode.next!=null)
  {
      currentNode=currentNode.next;
      }

    currentNode.next=node;
    return head;
   }
 /**
  * Print All Elements of Linked List
  *
  * @param      Head of Linked List
  */
  public static void printList(Node node) {
   Node currentNode = node;
   while (currentNode != null) {
    System.out.println(currentNode.data);
    currentNode = currentNode.next;
   }


  }
}

Detect and Remove Loop in a Linked List

Detect & Remove Loop  From Linked List :
class DetectAndRemoveLoop
{
  static class Node {
   int data;
   Node next;
  }

  public static void main(String[] args) {
    Node node1 = new Node();
     node1.data = 1;
     Node node2 = new Node();
     node2.data = 2;
     Node node3 = new Node();
     node3.data = 3;
     Node node4 = new Node();
      node4.data = 4;
      Node node5 = new Node();
      node5.data = 5;
      Node node6 = new Node();
      node6.data = 6;
      Node node7 = new Node();
      node7.data = 7;
     node1.next = node2;
     node2.next = node3;
     node3.next = node4;
     node4.next=node5;
     node5.next=node6;
     node6.next=node7;
     node7.next=node3;
    if (detectAndRemove(node1) == true)
    {  printList(node1);  }
    else
    {System.out.println("No Loop Found"); }
  }
public  static boolean detectAndRemove(Node head)
  {
    Node slow=head;
    Node fast=head.next;
    while(fast!=null && fast.next!=null)
    {
      if(slow==fast)
      {
        break;
      }
    slow=slow.next;
    fast=fast.next.next;
  }
    if(slow==fast)
    {  slow=head;
        while(fast.next!=slow)
        {
          slow=slow.next;
          fast=fast.next;
        }
        fast.next=null;
        System.out.println("Loop Removed");
        return true;
    }

    return false;
  }

  public static void printList(Node node) {
   Node currentNode = node;
   while (currentNode != null) {
    System.out.println(currentNode.data);
    currentNode = currentNode.next;
   }
}
}

Swap Nodes in the Linked List

Swap nodes in a linked list without swapping data :
 
class SwapNodes
{
  static class Node{
    int data;
    Node next;
  }
  public static void main(String[] args) {
    Node head = new Node();
     head.data = 1;
     Node node2 = new Node();
     node2.data = 2;
     Node node3 = new Node();
     node3.data = 3;
     Node node4 = new Node();
      node4.data = 4;
      Node node5 = new Node();
      node5.data = 5;
      Node node6 = new Node();
      node6.data = 6;
      Node node7 = new Node();
      node7.data = 7;
     head.next = node2;
     node2.next = node3;
     node3.next = node4;
     node4.next=node5;
     node5.next=node6;
     node6.next=node7;
     node7.next=null;
     head=swap(head,2,6);
     //head=swap(head,2,3);
     printList(head);
  }
public static Node  swap(Node head , int x , int y)
{
        if(x==y)
          return head;

        // Search for x (keep track of prevX and CurrX)
        Node prevX = null, currX = head;
        while (currX != null && currX.data != x)
        {
            prevX = currX;
            currX = currX.next;
        }

        // Search for y (keep track of prevY and currY)
       Node prevY = null, currY = head;
       while (currY != null && currY.data != y)
       {
           prevY = currY;
           currY = currY.next;
       }

       // If either x or y is not present, nothing to do
        if (currX == null || currY == null)
            return head;

            // If x is not head of linked list
            if (prevX != null)
                prevX.next = currY;
            else //make y the new head
                head = currY;

            // If y is not head of linked list
            if (prevY != null)
                prevY.next = currX;
            else // make x the new head
                head = currX;

            // Swap next pointers
            Node temp = currX.next;
            currX.next = currY.next;
            currY.next = temp;
            return head;

}


            /**
 * Print All Elements of Linked List
 *
 * @param      Head of Linked List
 */
 public static void printList(Node node) {
  Node currentNode = node;
  while (currentNode != null) {
   System.out.println(currentNode.data);
   currentNode = currentNode.next;
  }
}
}