In a previous couple of posts, We Learned How to remove/delete first and the last Node from Singly Linked List.
- How to remove the last node from a Singly Linked List in Java
- How to remove the first node from a Singly Linked List in Java ?
In this post, We will learn How to remove/delete a node from a Singly Linked List at a given position in Java?
Program Logic:
- if position == 0 then move head node to next node (head = head.next)
- if position < 0 Or position > = number of nodes in Linked List then throws an Exception
- We have to set a pointer to the node previous to the node to be deleted. So if the position is not zero then we have to run a loop position-1 times and get a pointer to the previous node.
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
package com.kkjavatutorials.util; import java.util.NoSuchElementException; /** * How to remove a node from a Singly 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 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; } } /** * Remove/delete the element at the specified position in this list. * I will Return the element that is removed from the list. * @param position the index of the element to be removed * @return the element removed Element * @throws IllegalArgumentException * * 0 --First Node * 1-- Second Node * 2--Third Node */ public T remove(int position) { T result = null; if(head == null) { throw new NoSuchElementException("List is Empty!!"); } /** * if insert position is negative then throw * IllegalArgumentException */ if(position<0) { throw new IllegalArgumentException("Invalid Value of position ="+position); } /** * if node insertion position is greater * than number of nodes then * throw IllegalArgumentException */ if(position > size()-1) { throw new IllegalArgumentException("Valus of position ="+position+" is greater than number of nodes in the list!!"); } if(position == 0) { head = head.next; result = head.data; return result; } Node<T> previousNode = head; int count = 0; while(count < position-1) { previousNode = previousNode.next; count++; } Node<T> currentNode = previousNode.next; previousNode.next = currentNode.next; result = currentNode.data; currentNode.next = null; return result; } /** * This method returns size/length of Linked List * @return size of Linked List */ public int size() { int length = 0; if(head == null) { return length; } Node<T> currentNode = head; while (currentNode != null) { length ++; currentNode = currentNode.next; } return length; } /** * 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; linkedList.remove(position); System.out.println("After removing an Element from given position = "+position+" LinkedList is:"); linkedList.displayLinkedList(); } } |
Output of this program:
Original LinkedList:
10 20 30 40 50 60
After removing an Element from given position = 2 LinkedList is:
10 20 40 50 60
You May Also Like:
- How to remove the last node from a Singly Linked List in Java
- How to remove the first node from a Singly Linked List in Java ?
That’s all about the How to delete a node from a Singly Linked List at a given position in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.