JAVA Program for Deletion from Beginning in a Doubly Linked List
JAVA Program to Delete a Node from the Beginning
Over here in this program we will create a linked list and add a node at the end of doubly linked list. To insert a new node at the end of a linked list we have to check that the list is empty or not. If the list is empty both head and tail points towards the newly added node. If it is not empty, in such case add a new node at the end and make sure that tail points towards the newly added node.
Steps to be followed while Inserting a Node at the Beginning of a Doubly Linked List
- Check for the presence of Node in the List, if there exists some Nodes, Continue.
- Now, to Delete a node from the end of the Doubly Linked List, we’ll have to create and redirect multiple links in the Linked List.
- Foremost, the link between the First Node and the head and the link between the Previous Pointer of the First Node and the null will also be broken.
- Now Since the Second Node is going to be the First Node of the Linked List. So, the Head will now Point to the address of the Second Node.
- Then the Previous Pointer of the Second Node will now point to Null.
Example:
If we have a list (10 –> 20 –> 30 –> 40) and we have to add a new node which is 50. So after adding new node. Updated Linked list will be (10 –> 20 –> 30 –>40 –> 50) . We will be learning more about the whole process in the explanation below.
Algorithm to be used for Deletion from the Beginning of a Doubly Linked List
- IF(HEAD == NULL)
- RETURN
- ELSE IF(HEAD != TAIL)
- TAIL = TAIL.PREV
- TAIL.NEXT = NULL
- ELSE
- HEAD = TAIL = NULL
Java Program for Deletion from the Beginning of a Doubly Linked List
import java.lang.*; class DoublyLinkedList { Node head; // not using parameterized constructor would by default // force head instance to become null // Node head = null; // can also do this, but not required // Node Class class Node { int data; Node next, prev; Node (int x) // parameterized constructor { data = x; next = null; prev = null; } } public void insertBeginning (int data) { // Creating newNode memory & assigning data value Node freshNode = new Node (data); freshNode.next = head; freshNode.prev = null; // if DLL had already >=1 nodes if (head != null) head.prev = freshNode; // changing head to this head = freshNode; } // function for deleting first node public void deleteInitial () { if (head == null) { System.out.println ("List is empty"); return; } else { //testing for the presence of a single node in the list, If not, Then head and tail will be re-directed if (head != null) { head = head.next; } //if only one node exist both head and tail will be redirected to null else { head = null; } } } void printList () { //Node current will point to head Node curr = head; if (head == null) { System.out.println ("List is empty"); return; } while (curr != null) { //Prints each node by increasing order of the pointer System.out.print (curr.data + " "); curr = curr.next; } System.out.println (); } } class Main { public static void main (String args[]) { DoublyLinkedList doublylist = new DoublyLinkedList (); doublylist.insertBeginning (3); doublylist.insertBeginning (2); doublylist.insertBeginning (1); doublylist.insertBeginning (4); doublylist.insertBeginning (7); System.out.println ("List before deletion : "); doublylist.printList (); doublylist.deleteInitial (); System.out.println ("List after deletion : "); doublylist.printList (); } }
Output
List before deletion : 7 4 1 2 3 List after deletion : 4 1 2 3
import java.lang.*; // Node Class class Node { int data; Node next, prev; Node(int x) // parameterized constructor { data = x; next = null; prev = null; } } class Main { static Node insertBeginning(Node head, int data) { // Creating newNode memory & assigning data value Node newNode = new Node(data); newNode.next = head; newNode.prev = null; // if DLL had already >=1 nodes if(head !=null) head.prev = newNode; // changing head to this head = newNode; return head; } // function for deleting first node static Node deleteInitial ( Node head) { if (head == null) { System.out.println ("List is empty"); return head; } else { //testing for the presence of a single node in the list, If not, Then head and tail will be re-directed if (head != null) { head = head.next; } //if only one node exist both head and tail will be redirected to null else { head = null; } } return head; } static void printList (Node head) { //Node current will point to head Node curr = head; if (head == null) { System.out.println ("List is empty"); return; } while (curr != null) { //Prints each node by increasing order of the pointer System.out.print (curr.data + " "); curr = curr.next; } System.out.println (); } // required for insertAfterPosition() method static int getLength(Node node){ int size=0; // traverse to the last node each time incrementing the size while(node!=null) { node = node.next; size++; } return size; } public static void main(String args[]) { Node head = null; head = insertBeginning(head,3); head = insertBeginning(head,2); head = insertBeginning(head,1); head = insertBeginning(head,4); head = insertBeginning(head,7); System.out.println ("List before deletion : "); printList (head); head = deleteInitial (head); System.out.println ("List after deletion : "); printList (head); } }
Output
List before deletion : 7 4 1 2 3 List after deletion : 4 1 2 3
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
Doubly Linked List
- Introduction to Doubly Linked list in Data Structure
Click Here - Doubly Linked List in –
- Insertion in doubly linked list –
- Insertion at beginning in doubly linked list –
- Insertion at end in doubly linked list –
- Insertion at nth node in doubly linked list –
- Deletion in doubly linked list –
- Deletion from beginning in doubly linked list :
- Deletion from nth in doubly linked list :
- Deletion from end in doubly linked list :
- Insertion and Deletion in a doubly linked list :
- Insertion in the middle in a doubly linked list :
Doubly Linked List
- Introduction to Doubly Linked list in Data Structure
- Doubly Linked List in – C | C++ | Java
- Insertion in doubly linked list – C | C++ | Java
- Deletion in doubly linked list – C | C++ | Java
- Insertion and Deletion in doubly linked list – C | C++ | Java
- Insertion in the middle in a doubly linked list – C | C++ | Java
Login/Signup to comment