Java Data Structures - Linked List Other Related Topics

Java - Insert a new node at a given position in the Linked List



In this method, a new element is inserted at the specified position in the linked list. For example - if the given List is 10->20->30 and a new element 100 is added at position 2, the Linked List becomes 10->100->20->30.

First, a new node with given element is created. If the insert position is 1, then the new node is made to head. Otherwise, traverse to the node that is previous to the insert position and check if it is null or not. In case of null, the specified position does not exist. In other case, assign next of the new node as next of the previous node and next of previous node as new node. The below figure describes the process, if the insert node is other than the head node.

Linked List - Insert Node

The function push_at is created for this purpose. It is a 6-step process.

void push_at(int newElement, int position) {     
  
  //1. allocate node to new element
  Node newNode = new Node(); 
  newNode.data = newElement;
  newNode.next = null;

  //2. check if the position is > 0
  if(position < 1) {
    System.out.print("\nposition should be >= 1.");
  } else if (position == 1) {
    
    //3. if the position is 1, make next of the
    //   new node as head and new node as head
    newNode.next = head;
    head = newNode;
  } else {
    
    //4. Else, make a temp node and traverse to the 
    //   node previous to the position
    Node temp = new Node();
    temp = head;
    for(int i = 1; i < position-1; i++) {
      if(temp != null) {
        temp = temp.next;
      }
    }
 
    //5. If the previous node is not null, make 
    //   newNode next as temp next and temp next 
    //   as newNode.
    if(temp != null) {
      newNode.next = temp.next;
      temp.next = newNode;  
    } else {
      
      //6. When the previous node is null
      System.out.print("\nThe previous node is null.");
    }       
  }
}  

The below is a complete program that uses above discussed concept to insert new node at a given position in the linked list.

//node structure
class Node {
    int data;
    Node next;
};

class LinkedList {
  Node head;

  LinkedList(){
    head = null;
  }

  //Add new element at the end of the list
  void push_back(int newElement) {
    Node newNode = new Node();
    newNode.data = newElement;
    newNode.next = null; 
    if(head == null) {
      head = newNode;
    } else {
      Node temp = new Node();
      temp = head;
      while(temp.next != null)
        temp = temp.next;
      temp.next = newNode;
    }    
  }

  //Inserts a new element at the given position
  void push_at(int newElement, int position) {     
    Node newNode = new Node(); 
    newNode.data = newElement;
    newNode.next = null;

    if(position < 1) {
      System.out.print("\nposition should be >= 1.");
    } else if (position == 1) {
      newNode.next = head;
      head = newNode;
    } else {
      
      Node temp = new Node();
      temp = head;
      for(int i = 1; i < position-1; i++) {
        if(temp != null) {
          temp = temp.next;
        }
      }
   
      if(temp != null) {
        newNode.next = temp.next;
        temp.next = newNode;  
      } else {
        System.out.print("\nThe previous node is null.");
      }       
    }
  } 

  //display the content of the list
  void PrintList() {
    Node temp = new Node();
    temp = this.head;
    if(temp != null) {
      System.out.print("The list contains: ");
      while(temp != null) {
        System.out.print(temp.data + " ");
        temp = temp.next;
      }
      System.out.println();
    } else {
      System.out.println("The list is empty.");
    }
  }    
};

// test the code
public class Implementation {
  public static void main(String[] args) {
    LinkedList MyList = new LinkedList();

    //Add three elements at the end of the list.
    MyList.push_back(10);
    MyList.push_back(20);
    MyList.push_back(30);
    MyList.PrintList();

    //Insert an element at position 2
    MyList.push_at(100, 2);
    MyList.PrintList();

    //Insert an element at position 1
    MyList.push_at(200, 1);
    MyList.PrintList();      
  }
}

The above code will give the following output:

The list contains: 10 20 30
The list contains: 10 100 20 30
The list contains: 200 10 100 20 30 

5