Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
C# Program For Deleting A Node In A Doubly Linked List
Next article icon

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. 

  1. If the node to be deleted is the head node then make the next node as head.
  2. 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.nextNode
  • Set 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!



Next Article
C# Program For Deleting A Node In A Doubly Linked List
author
kartik
Improve
Article Tags :
  • C Programs
  • DSA
  • Linked List
  • Amazon
  • doubly linked list
Practice Tags :
  • Amazon
  • Linked 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 Detecting Loop In A Linked List
    Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. Solution: Floyd's Cycle-Finding Algorithm Approach: This is the fastest method and has been described below: Traverse linked list using two pointers.Move one pointer(slow_p) by one and anoth
    2 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 Inserting A Node In A Linked List
    We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. C/C++ Code // A linked list node struct Node { int data; struct N
    7 min read
  • C Program For Finding Length Of A Linked List
    Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1->3->1->2->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initia
    2 min read
  • C Program For Segregating Even And Odd Nodes In A Linked List
    Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers the same. Examples: Input: 17->15->8->12->10->5->4->1->7->6-
    4 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/C++
    4 min read
  • C Program For Finding The Middle Element Of A Given Linked List
    Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences