In his post, We will learn How to Remove/Delete the first node in a Doubly Linked List in Java?
Logic is Very Simple:
- if List is Empty than throws NoSuchElementException Exception
- if List is not empty then head we will assign to temp Node
- if head == tail that means list has only one Node so nullify tail node
- if List has more than one Node then nullify head.next.previous so that first’s node next and next Node’s previous pointer disconnects.
3. Afterward, the head’s move to the next pointer and temp’s next pointer nullify.
Below is the complete Source code:
LinkedList.java
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 |
package com.kkjavatutorials.util; import java.util.NoSuchElementException; /** * @author KK JavaTutorials * Custom Doubly LinkedList Implementation * In Java */ public class LinkedList<T> { //Pointer to first node private Node<T> head; //Pointer to last node. private Node<T> tail; private int size; /** * @author KK JavaTutorials * This internal class represent * Node of Doubly Linked List */ private static class Node<E> { private E data; private Node<E> next; private Node<E> previous; public Node(E data) { this.data = data; } } /** * Method to insert at the End of * a Doubly Linked List * @param e Element to be inserted */ public void addLast(T e) { final Node<T> newNode = new Node<>(e); /** * if head is null that means LinkedList is empty so * make newNode as head Node * else newNode assign to tail's next and assign * tail to newNode's previous * finally assign newNode to tail */ if (getSize() == 0) head = newNode; else { tail.next = newNode; newNode.previous = tail; } tail = newNode; size++; } /** * Remove/Delete and returns the first element from this list. * @return the first element from this list * @throws NoSuchElementException if this list is empty */ public T removeFirst() { //If List is Empty than throws an exception if(getSize() == 0) { throw new NoSuchElementException("List is Empty!!"); } Node<T> temp = head; if(head == tail) { tail = null; }else { head.next.previous = null; } head = head.next; T data = temp.data; temp.next = null; return data; } /** * Linked List Printing in forward direction */ public void displayLinkedList() { Node<T> temp = head; if(getSize() == 0) { return; }else { while(temp != null) { System.out.print(temp.data+" "); temp = temp.next; } } } public int getSize() { return size; } } |
ClientTest.java
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 |
package com.kkjavatutorials.client; import com.kkjavatutorials.util.LinkedList; /** * Client program to show demo * Demo to delete first node in a Doubly * Linked List in Java * @author KK JavaTutorials * */ public class ClientTest { public static void main(String[] args) { //Create an Instance of LinkedList LinkedList<Integer> linkedList = new LinkedList<>(); //Call addFirst few times linkedList.addLast(10); linkedList.addLast(20); linkedList.addLast(30); System.out.println("-----Original LinkedList-------"); linkedList.displayLinkedList(); System.out.println(); Integer removeFirst = linkedList.removeFirst(); System.out.println("Removed Element::"+removeFirst); System.out.println("----------After Deleting First Node from Linked List---------------"); linkedList.displayLinkedList(); } } |
The output of this Program:
—–Original LinkedList——-
10 20 30
Removed Element::10
———-After Deleting First Node from Linked List—————
20 30
You May Also Like:
How to represent the Doubly Linked List in Java?
How to insert a node at the beginning of a Doubly Linked List in Java ?
How to insert a node at the end of a Doubly Linked List in Java ?
Representation of Singly Linked List in Java ?
How to insert a node at the beginning of 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 in Linked List at a given position in Java ?
How to remove the first node from a Singly Linked List in Java ?
How to remove the last node from a Singly Linked List in Java
How to remove a node from a Singly Linked List at a given position in Java?
How to remove a given key from the Singly Linked List in Java ?
How to find the middle node in a Singly Linked List in Java ?
How to search an element in a Singly Linked List in Java ?
That’s all about the How to delete/Remove the first node in a Doubly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.