Insertion in Linked List in Java
How to insert an element in a Linked List
Insertion in a Singly Linked List is one of the primary operation that we can perform on Singly Linked List in Java. Singly Linked List is a structure that is made up of nodes, which is enabled to store data and the address of the next node.
In this article we will learn about all the types of insertion in linked list.
How do you add an element to a Singly Linked List ?
Adding data in a Singly Linked List is practically a bit easier than the other data structures. As in Singly Linked List, we does not need to traverse the whole data structure for entering a node, we just need to update the address of the last node which is currently pointing to null, or some other node, with the new node that we want to insert. There are three different ways in which we can insert a node in a linked list -:
- Insertion at beginning
- Insertion at end
- Insertion at nth position
Definition of Singly linked list in JAVA
// Node Class class Node{ int data; Node next; Node(int x) // parameterized constructor { data = x; next = null; } };
Inserting an element in the beginning of the Singly Linked List
To perform insertion at beginning in singly linked list we will use the following steps:-
- We will create a space at beginning, before head node.
- In this space created we will built a new node.
- After that we will but the data in the new node that we want to insert.
Method 1: Calls functions using object
Method 2: Calls static functions from main
public Node insertBeginning(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; return head; }
static Node insertBeginning(Node head, int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; return head; }
Singly Linked List insertion in Java at the End
To perform Singly Linked List insertion in Java at the end in a singly linked list we will use the following steps:-
- In the process of Insertion at the Last Position we’ll begin by checking whether if the linked list is empty.
- If the head is equal to null, then the list is already empty else the list already has an element.
- In Order to insert a node in the end we’ll first store the current pointer address of null from the current last node to a temporary variable.
- The value of that temporary variable will now be stored in the address of the new node being inserted.
- Followed by which we’ll store the address of the new node into the previous last node of the linked list.
public void insertEnd(int data) { Node newNode = new Node(data); if(head==null) { head = newNode; return; } Node temp = head; while(temp.next!=null) temp = temp.next; temp.next = newNode; }
static Node insertEnd(Node head, int data) { Node newNode = new Node(data); if(head==null) { head = newNode; return head; } Node temp = head; while(temp.next!=null) temp = temp.next; temp.next = newNode; return head; }
Inserting an element in the nth position of the Singly Linked List
To perform insertion at a specific position in singly linked list we will use the following steps:-
- First we will create a new node named by newnode and put the position where you want to insert the node.
- Now give the address of the new node in previous node means link the new node with previous node.
- After this, give the address of current node in new node. Means link your new node also with current node.
public void insertAfter(int n,int data) { int size = calcSize(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if(n < 1 || n > size) { System.out.println("Can't insert\n"); } else { Node newNode = new Node(data); // required to traverse Node temp = head; // traverse to the nth node while(--n > 0) temp=temp.next; newNode.next= temp.next; temp.next = newNode; } }
static Node insertAfter(int pos, int data, Node head) { int size = calcSize(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if (pos < 1 || size < pos) { System.out.println("Can't insert," + pos + " is not a valid position\n"); } else { Node newNode = new Node(data); // required to traverse Node Node temp = head; // traverse to the nth node while (--pos > 0) temp = temp.next; newNode.next = temp.next; temp.next = newNode; } return head; }
Full Code for Insertion in Java
Let us have a look at the code below –
Runimport java.lang.*; class LinkedList { Node head; // Node Class class Node{ int data; Node next; Node(int x) { data = x; next = null; } } public Node insertBeginning(int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; return head; } public void insertEnd(int data) { Node newNode = new Node(data); if(head==null) { head = newNode; return; } Node temp = head; while(temp.next!=null) temp = temp.next; temp.next = newNode; } public void insertAfter(int n,int data) { int size = calcSize(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if(n < 1 || n > size) { System.out.println("Can't insert\n"); } else { Node newNode = new Node(data); // required to traverse Node temp = head; // traverse to the nth node while(--n > 0) temp=temp.next; newNode.next= temp.next; temp.next = newNode; } } public void display() { Node node = head; //as linked list will end when Node reaches Null while(node!=null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } public int calcSize(Node node){ int size = 0; while(node!=null){ node = node.next; size++; } return size; } } class Main{ public static void main(String args[]) { LinkedList listObj = new LinkedList(); listObj.insertBeginning(15); listObj.insertBeginning(10); listObj.insertBeginning(5); listObj.display(); listObj.insertEnd(20); listObj.insertEnd(25); listObj.insertEnd(30); listObj.insertEnd(35); listObj.display(); listObj.insertAfter(3,100); listObj.display(); } }
Output
5 10 15 5 10 15 20 25 30 35 5 10 15 100 20 25 30 35
Runimport java.lang.*; // Node Class class Node { int data; Node next; Node(int x) { data = x; next = null; } } class Main { static Node insertBeginning(Node head, int data) { Node newNode = new Node(data); newNode.next = head; head = newNode; return head; } static Node insertEnd(Node head, int data) { Node newNode = new Node(data); if(head==null) { head = newNode; return head; } Node temp = head; while(temp.next!=null) temp = temp.next; temp.next = newNode; return head; } static Node insertAfter(int pos, int data, Node head) { int size = calcSize(head); // Can only insert after 1st position // Can't insert if position to insert is greater than size of Linked List if(pos < 1 || size < pos) { System.out.println("Can't insert," + pos + " is not a valid position\n"); } else { Node newNode = new Node(data); // required to traverse Node Node temp = head; // traverse to the nth node while (--pos > 0) temp = temp.next; newNode.next= temp.next; temp.next = newNode; } return head; } static void display(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } static int calcSize(Node node){ int size=0; while(node!=null) { node = node.next; size++; } return size; } public static void main(String args[]) { Node head = null; head = insertBeginning(head,15); head = insertBeginning(head,10); head = insertBeginning(head,5); display(head); head = insertEnd(head,20); head = insertEnd(head,25); head = insertEnd(head,30); head = insertEnd(head,35); display(head); //Inserts after 3rd position head = insertAfter(3,100,head); display(head); } }
Output
5 10 15 5 10 15 20 25 30 35 5 10 15 100 20 25 30 35
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment