In the previous three posts, We Learned How to remove/delete first and last Node from Singly Linked List.
We also learned how to remove Linked list Node from a given position or Index.
- 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 ?
- How to remove a node from a Singly Linked List at a given position in Java?
In this post, We will learn How to remove a given key from the Singly Linked List in Java?
Program Logic:
- if position == 0 then move head node to next node (head = head.next) and return true
- We have to traverse the list from the head to the last node and compare each node value with the supplied key if found then delete that Node using Step-3 and return true else return false.
- We have to set a pointer to the node previous to the node to be deleted. So we have to run a loop till the last node and get a pointer to the previous node.
Complete Source code :
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 |
package com.kkjavatutorials.util; import java.util.NoSuchElementException; /** * How to remove a given key from the Singly Linked List 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; } } /** * It will remove the first occurrence of the specified * element from this list,if it is present. * @param key Element has to remove from list. * @return if Element found and removed then return true else false */ public boolean remove(T key) { boolean isFound = false; if(head == null) { throw new NoSuchElementException("List is Empty!!"); } if(head.data == key) { head = head.next; return true; } Node<T> currentNode = head; Node<T> previousNode = null; while(currentNode !=null) { if(currentNode.data == key) { isFound = true; break; } previousNode = currentNode; currentNode = currentNode.next; } if(currentNode == null) { return isFound; } currentNode = previousNode.next; previousNode.next = currentNode.next; currentNode.next = null; return isFound; } /** * 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 key = 400; if (linkedList.remove(key)) { System.out.println("After removing an Element = " + key + " LinkedList is:"); linkedList.displayLinkedList(); } else System.out.println(key + " not found in List"); } } |
Output of this program :
Original LinkedList:
10 20 30 40 50 60
After removing an Element = 40 LinkedList is:
10 20 30 50 60
You May Also Like:
- Write a program to move all zero at the end of the array with/without using existing data structures
- Write a program to move all negative elements to end in an int array ?
- There is a stream of words which contains Anagrams. How would you print anagrams in a single bucket from that stream?
- How to insert a node in Linked List at a given position in Java ?
That’s all about the How to remove a given key from the Singly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.