In this post, we will learn How to insert a node in Linked List at a given position in Java?
Program Logic Explanation
Case 1: If the insertion position is zero(o) then assign head to newNode.next and newNode to head.
Case 2: If the insertion position >0 then
- We have to traverse the Linked list upto position-1 nodes.
- Once we traverse the position-1 nodes then allocate memory and the given data to the new node.
- Assign the next pointer of the new node to the next of the current node.
- Assign the next pointer of the current node to the new node.
Exception Handling:
Throw Exception if node insert position is less than zero.
Throw Exception if node insertion position is greater than the number of nodes in the Linked List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
package com.kkjavatutorials.util; /** * How to insert a node in Linked List at a given position in Java ? * @author KK JavaTutorials */ public class LinkedList<T> { private Node<T> head; //This internal class represents Node in Linked List private static class Node<T>{ private T data; private Node<T> next; public Node(T data) { super(); this.data = data; this.next = null; } } /** * This method insert a node at given position * (Position start from zero(0)) * @param data Node value has to insert * @param position */ public void insertAtPosition(T data,int position) { /** * if insert position is negative then throw * IllegalArgumentException */ if(position<0) { throw new IllegalArgumentException("Invalid Value of position ="+position); } Node<T> newNode = new Node<T>(data); if(position == 0) { newNode.next = head; head = newNode; return; } Node<T> currentNode = head; for (int i = 0; i < position-1; i++) { currentNode = currentNode.next; /** * if node insertion position is greater * than number of nodes then * throw IllegalArgumentException */ if(currentNode == null) { throw new IllegalArgumentException("Valus of position ="+position+" is greater than number of nodes in the list!!"); } } newNode.next = currentNode.next; currentNode.next = newNode; } /** * This method insert a Node in Linked List * @param data has to insert in the List */ public void insert(T data) { Node<T> newNode = new Node<T>(data); if(head == null) { head = newNode; }else { Node<T> currentNode = head; while (currentNode.next != null) { currentNode = currentNode.next; } currentNode.next = newNode; } } /** * Method which traverse Linked List and display all data. */ public void displayLinkedList() { Node<T> currentNode = head; while(currentNode!= null) { System.out.print(currentNode.data+" "); currentNode = currentNode.next; } } public static void main(String[] args) { LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.insert(10); linkedList.insert(20); linkedList.insert(30); linkedList.insert(40); linkedList.insert(50); linkedList.insert(60); System.out.println("Original LinkedList:"); linkedList.displayLinkedList(); System.out.println(); int position = 2; int nodeValue = 80; System.out.println("Adding a Node with Value:"+nodeValue+" at position:"+position); linkedList.insertAtPosition(nodeValue, position); linkedList.displayLinkedList(); } } |
Output of this program:
Original LinkedList:
10 20 30 40 50 60
Adding a Node with Value:80 at position:2
10 20 80 30 40 50 60
You May Also Like:
- How to search an element in a Singly Linked List in Java ?
- How to find middle node in a Singly Linked List in Java ?
- How to Insert node at the end of a Singly Linked List in Java ?
- How to insert a node at the beginning of a Singly Linked List in Java?
- Representation of Singly Linked List in Java ?
- How to check whether a linked list has a loop/cycle in java ?
- Queue Implementation using LinkedList in java
- Queue Implementation using an array in java
That’s all about the How to insert a node in Linked List at a given position in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.