Monday 30 May 2016

Linked List Node Deletion from Beginning

Linked List Delete Node from Beginning :

  • JAVA  : 

/**
 * @author     Anurag Goel
 */
class DeleteAtBegin {
 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;
  node1.next = node2;
  node2.next = node3;
  node3.next = null;
  printList(delete(node1));
 }

 public static Node delete(Node head) {
  Node temp= head;
  Node currentNode= temp.next;
  temp=null;
  return currentNode;

 }


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


 }
}

Reverse Linked List (In Place) - Recursive Method :

Reverse Linked List In Place  using Recursive Method  :

  • JAVA  : 


/**
 *@author     Anurag Goel
 * Reverse Linked List Using Recursion
 */
public class ReverseLinkedListR{

            static class Node
            {
                        int data;
                        Node next;
            }
           
            /**
             * Main Method
             *
             * @param      args  The args
             */
            public static void main(String[] args) {
                        Node head = new Node();
                        head.data=1;
                        Node node1 = new Node();
                        node1.data=2;
                        Node node2 = new Node();
                        node2.data=3;
                        Node node3 = new Node();
                        node3.data=4;
                        head.next=node1;
                        node1.next=node2;
                        node2.next=node3;
                        node3.next=null;
                        printList(Reverse(head));
                       
            }

/**
 * Reverse Linked List
 *
 * @param      head  The head
 */
            private static Node Reverse(Node head)
            {
                        if(head==null || head.next==null)
                                    return head;
                        Node nextNode = head.next;
                        head.next=null;
                        Node node = Reverse(nextNode);
                        nextNode.next=head;
                        return node;

            }

            /**
 * 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;
  }
}

}

Reverse Linked List (InPlace) - Iteration Method

.
  • JAVA  : 

/**
 *@author     Anurag Goel
 * Reverse Linked List  Iteration Method
 */
public class ReverseLinkedList{

            static class Node
            {
                        int data;
                        Node next;
            }
           
            /**
             * Main Method
             *
             * @param      args  The args
             */
            public static void main(String[] args) {
                        Node head = new Node();
                        head.data=1;
                        Node node1 = new Node();
                        node1.data=2;
                        Node node2 = new Node();
                        node2.data=3;
                        Node node3 = new Node();
                        node3.data=4;
                        head.next=node1;
                        node1.next=node2;
                        node2.next=node3;
                        node3.next=null;
                        printList(Reverse(head));      
            }

/**
 * Reverse Linked List
 *
 * @param      head  The head
 */
            private static Node Reverse(Node head)
            {
                        Node nextNode=null, temp=null;
           
                        while(head!=null)
                        {
                                    nextNode=head.next;
                                    head.next=temp;
                                    temp=head;
                                    head=nextNode;
                        }
                        return temp;

            }

            /**
 * 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;
  }
}

}

Linked List Traversal

.
  • JAVA  : 
/**
 *@author     Anurag Goel
 * Print All Elements of Linked List
 */
class PrintElements {
 static class Node {
  int data;
  Node next;
 }
 /**
  * Main Method
  */
 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;
  node1.next = node2;
  node2.next = node3;
  node3.next = null;
  printList(node1);

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


 }
}

Linked List Insertion at the End.

  • JAVA  : 
/**
 * @author     Anurag Goel
 * Example : Insert At End of Linked List
 */
class InsertAtEnd{
 static class Node {
  int data;
  Node next;
 }
 /**
  * Main Method
  */
 public static void main(String args[]) {
  Node node = insert(insert(insert(insert(null, 1), 2), 3),4);
  printList(node);

 }
/**
 * 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      node  The node
 */
 public static void printList(Node node) {
  Node currentNode = node;
  while (currentNode != null) {
   System.out.println(currentNode.data);
   currentNode = currentNode.next;
  }


 }