C Program For Deleting A Node In A Doubly Linked List Last Updated : 25 Apr, 2023 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 C // C program to implement // the above approach #include <stdio.h> #include <stdlib.h> // A node of the doubly linked list struct Node { int data; struct Node* next; struct Node* prev; }; /* Function to delete a node in a Doubly Linked List. head_ref --> pointer to head node pointer. del --> pointer to node to be deleted. */ void deleteNode(struct Node** head_ref, struct Node* del) { // Base case if (*head_ref == NULL || del == NULL) return; // If node to be deleted is head node if (*head_ref == del) *head_ref = 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 free(del); return; } // UTILITY FUNCTIONS /* Function to insert a node at the beginning of the Doubly Linked List */ void push(struct Node** head_ref, int new_data) { // Allocate node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // Put in the data new_node->data = new_data; /* Since we are adding at the beginning, prev is always NULL */ new_node->prev = NULL; // Link the old list of the new node new_node->next = (*head_ref); // Change prev of head node to new node if ((*head_ref) != NULL) (*head_ref)->prev = new_node; // Move the head to point to the new node (*head_ref) = new_node; } /* Function to print nodes in a given doubly linked list. This function is same as printList() of singly linked list */ void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } // Driver code int main() { // Start with the empty list struct Node* head = NULL; /* Let us create the doubly linked list 10<->8<->4<->2 */ push(&head, 2); push(&head, 4); push(&head, 8); push(&head, 10); printf( "Original Linked list "); printList(head); /* Delete nodes from the doubly linked list */ // Delete first node deleteNode(&head, head); // Delete middle node deleteNode(&head, head->next); // Delete last node deleteNode(&head, head->next); /* Modified linked list will be NULL<-8->NULL */ printf( "Modified Linked list "); printList(head); getchar(); } 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 C Program For Deleting A Node In A Doubly Linked List kartik Follow Improve Article Tags : Linked List C Programs DSA Amazon doubly linked list +1 More Practice Tags : AmazonLinked List Similar Reads C# Program For Deleting A Node In A Doubly Linked List 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 4 min read C 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 C 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 C 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 C 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. C #inc 4 min read C Program For Writing A Function To Delete A Linked List Algorithm For C:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C // C program to delete a linked list #include<stdio.h> #include<stdlib.h> #includ 2 min read How to Create a Doubly Linked List in C? A doubly linked list is a type of linked list in which each node contains a pointer to both the next node and the previous node. This allows traversal in both forward and backward directions. Each node in a doubly linked list stores data, a pointer to the next node, and a pointer to the previous nod 4 min read C 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, 3 min read Program for all operations on Circular Linked List in C In a Circular linked list, every element has a link to its next element in the sequence, and the last element has a link to the first element. A circular linked list is similar to the singly linked list except that the last node points to the first node. Below is the image to illustrate the same: 1. 11 min read C 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 Like