Question:
How to Delete Node from a Doubly Linked List?

Summary:

The task is to delete a node from a given position in a doubly linked list. Through this code, we define a method deleteNode within a class that facilitates the deletion of a node at a specified position in a doubly linked list, ensuring proper adjustment of pointers to maintain list integrity. The function takes the head of the list and the position as parameters, effectively deleting the targeted node and returning the updated head reference.


Solution:

//full function


class Solution

{

    public:

    //Function to delete a node at given position.

    Node* deleteNode(struct Node *head_ref,int x)

    {

        //initializing del node with head_ref.

        struct Node *del = head_ref;

        x = x-1;


        //iterating till the position given in parameter.

        while(x--)

            del = del->next;


        /* base case */

        //if head_ref or del is null, return null.

        if(head_ref == NULL || del == NULL)

            return NULL;


        /* If Node to be deleted is head Node */

        //if del is equal to head_ref, update head_ref to next 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 is not null, update previous node of del next to del previous.

        if(del->next != NULL)

            del->next->prev = del->prev;


        /* Change prev only if Node to be deleted is NOT the first Node */

        //if del previous is not null, update next node of del previous to del next.

        if(del->prev != NULL)

            del->prev->next = del->next;


        /* Finally, free the memory occupied by del*/

        //free up the memory of del node.

        free(del);

        //return updated head_ref.

        return head_ref;

    }     

};


Explanation:

  • The deleteNode function takes a doubly linked list's head (head_ref) and the position (x) of the node to be deleted.

  • It initializes a pointer del with head_ref and iterates through the list until it reaches the node to be deleted.

  • If the head or del is null, it returns null, indicating an invalid operation.

  • If the node to be deleted is the head node, it updates head_ref to the next node.

  • It then adjusts the prev and next pointers of the neighboring nodes to bypass the node to be deleted.

  • Finally, it frees the memory occupied by the node to be deleted (del) and returns the updated head_ref.


Answered by: >shivanshudfn74

Credit:> >Geeksforgeeks


Suggested blogs:

>How .transform handle the splitted groups?

>Can I use VS Code's launch config to run a specific python file?

>Python: How to implement plain text to HTML converter?

>How to write specific dictionary list for each cycle of loop?

>Reading a shapefile from Azure Blob Storage and Azure Databricks

>How to replace a value by another in certain columns of a 3d Numpy array?

>How to fix "segmentation fault" issue when using PyOpenGL on Mac?


Submit
0 Answers