Java Program For Deleting A Node In A Doubly Linked List Last Updated : 15 Dec, 2021 Comments Improve Suggest changes Like Article Like Report Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list can be divided into three main categories: After the deletion of the head node. After the deletion of the middle node. After the deletion of the last node. All three mentioned cases can be handled in two steps if the pointer of the node to be deleted and the head pointer is known. If the node to be deleted is the head node then make the next node as head.If a node is deleted, connect the next and previous node of the deleted node. Algorithm Let the node to be deleted be del.If node to be deleted is head node, then change the head pointer to next current head.if headnode == del then headnode = del.nextNodeSet next of previous to del, if previous to del exists.if del.nextNode != none del.nextNode.previousNode = del.previousNode Set prev of next to del, if next to del exists.if del.previousNode != none del.previousNode.nextNode = del.next Java // Java program to delete a node from // Doubly Linked List // Class for Doubly Linked List public class DLL { // Head of list Node head; // Doubly Linked list Node class Node { int data; Node prev; Node next; // Constructor to create a new // node. next and prev is by // default initialized as null Node(int d) { data = d; } } // Adding a node at the front of // the list public void push(int new_data) { // 1. Allocate node // 2. Put in the data Node new_Node = new Node(new_data); // 3. Make next of new node as head // and previous as NULL new_Node.next = head; new_Node.prev = null; // 4. Change prev of head node to // new node if (head != null) head.prev = new_Node; // 5. Move the head to point to the // new node head = new_Node; } // This function prints contents of // linked list starting from the given node public void printlist(Node node) { Node last = null; while (node != null) { System.out.print(node.data + " "); last = node; node = node.next; } System.out.println(); } // Function to delete a node in a Doubly // Linked List. head_ref --> pointer to // head node pointer. del --> data of node // to be deleted. void deleteNode(Node del) { // Base case if (head == null || del == null) { return; } // If node to be deleted is head node if (head == del) { head = del.next; } // Change next only if node to be // deleted is NOT the last node if (del.next != null) { del.next.prev = del.prev; } // Change prev only if node to be // deleted is NOT the first node if (del.prev != null) { del.prev.next = del.next; } // Finally, free the memory occupied // by del return; } // Driver Code public static void main(String[] args) { // Start with the empty list DLL dll = new DLL(); // Insert 2. So linked list becomes // 2->NULL dll.push(2); // Insert 4. So linked list becomes // 4->2->NULL dll.push(4); // Insert 8. So linked list becomes // 8->4->2->NULL dll.push(8); // Insert 10. So linked list becomes // 10->8->4->2->NULL dll.push(10); System.out.print("Created DLL is: "); dll.printlist(dll.head); // Deleting first node dll.deleteNode(dll.head); // List after deleting first node // 8->4->2 System.out.print( "List after deleting first node: "); dll.printlist(dll.head); // Deleting middle node from 8->4->2 dll.deleteNode(dll.head.next); System.out.print( "List after Deleting middle node: "); dll.printlist(dll.head); } } Output: Original Linked list 10 8 4 2 Modified Linked list 8 Complexity Analysis: Time Complexity: O(1). Since traversal of the linked list is not required so the time complexity is constant.Space Complexity: O(1). As no extra space is required, so the space complexity is constant. Please refer complete article on Delete a node in a Doubly Linked List for more details! Comment More infoAdvertise with us Next Article Java Program For Deleting A Node In A Doubly Linked List kartik Follow Improve Article Tags : Linked List Java Programs DSA Amazon doubly linked list +1 More Practice Tags : AmazonLinked List Similar Reads Java Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Java Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Java Program For Deleting A Given Node In Linked List Under Given Constraints Given a Singly Linked List, write a function to delete a given node. Your function must follow the following constraints: It must accept a pointer to the start node as the first parameter and node to be deleted as the second parameter i.e., a pointer to the head node is not global.It should not retu 3 min read Java Program For Deleting Last Occurrence Of An Item From Linked List Using pointers, loop through the whole list and keep track of the node prior to the node containing the last occurrence key using a special pointer. After this just store the next of next of the special pointer, into to next of special pointer to remove the required node from the linked list. Java / 6 min read Java Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, 5 min read Java Program For Writing A Function To Delete A Linked List Algorithm For Java:In Java, automatic garbage collection happens, so deleting a linked list is easy. Just need to change head to null. Implementation: Java // Java program to delete a linked list class LinkedList { // Head of the list Node head; // Linked List node class Node { int data; Node next; 2 min read Java Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read Java Program to Delete a Node From the Ending of the Circular Linked List In this article, we will learn about deleting a node from the ending of a circular linked list. Consider the linked list as shown below:Â Example:Input : 5->3->4->(head node)Output: 5->3->(head node)We will first initialize the list and add some data into it with the addNode() method a 3 min read Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Java Program to Delete a Node From the Middle of the Circular Linked List In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list. Algorithm C 4 min read Like