How do you delete an element from the middle of a Doubly Linked List?

Delete 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

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 given linked list is 1->2->3->4->5->6 then it should be modified to 1->2->3->5->6.
If the input linked list is NULL, then it should remain NULL.

If the input linked list has 1 node, then this node should be deleted and a new head should be returned.

C Exercises: Delete node from middle of a doubly linked list

Last update on December 20 2021 09:10:13 [UTC/GMT +8 hours]

About Linked List

Linked List is a one of the DataStructure used for storing collection of data. A Linked List has the following properties:

  1. A Linked List is linear dynamic DataStructure.
  2. The number of nodes in Linked List is not fixed, it can grow and shrink on demand.
  3. Each node of the Linked List is made up of two items - the data and a reference to the next node.
  4. The Last node has reference to null.
  5. The entry point is called head
  6. If the list is empty then the head is null reference.

Basic Structure of Linked List-

Delete a node in a Doubly Linked List in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to learn how to delete a node in doubly linked list.

Let's see the steps to solve the problem.

  • Write struct with data, prev and next pointers.

  • Write a function to insert the node into the doubly linked list.

  • Initialize the doubly linked list with dummy data.

  • Take a node to delete.

  • Write a function to delete the node. Consider the following three cases while deleting the node.

    • If the node is head node, then move the head to next node.

    • If the node is middle node, then link the next node to the previous node

    • If the node is end node, then remove the previous node link. Let's see the code.

Video liên quan

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề