Delete a Doubly Linked List node at a given position (original) (raw)

Last Updated : 31 Aug, 2024

Given a doubly linked list and a position **pos, the task is to delete the node at the given position from the beginning of Doubly Linked List.

**Input: LinkedList: 1<->2<->3, pos = 2
**Output: LinkedList: 1<->3

**Input: LinkedList: 1<->2<->3, pos = 1
**Output: LinkedList: 2<->3

Deletion-at-a-Specific-Position-in-Doubly-Linked-List

Delete Node at position 2 in Doubly Linked List

To delete a node at a specific position in doubly linked list, we can use the following steps:

// C++ Program to delete node at a specific position // in Doubly Linked List

#include using namespace std;

class Node { public: int data; Node * prev; Node * next; Node(int d) { data = d; prev = next = NULL; } };

// Function to delete a node at a specific position // in the doubly linked list Node * delPos(Node* head, int pos) {

// If the list is empty
if (head == NULL)
    return head;

Node * curr = head;

// Traverse to the node at the given position
for (int i = 1; curr != NULL && i < pos; ++i) {
    curr = curr -> next;
}

// If the position is out of range
if (curr == NULL)
    return head;

// Update the previous node's next pointer
if (curr -> prev != NULL)
    curr -> prev -> next = curr -> next;

// Update the next node's prev pointer
if (curr -> next != NULL)
    curr -> next -> prev = curr -> prev;

// If the node to be deleted is the head node
if (head == curr)
    head = curr -> next;

// Deallocate memory for the deleted node
delete curr;
return head;

}

void printList(Node * head) { Node * curr = head; while (curr != nullptr) { cout << curr -> data << " "; curr = curr -> next; } cout << endl; }

int main() {

// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node * head = new Node(1);
head -> next = new Node(2);
head -> next -> prev = head;
head -> next -> next = new Node(3);
head -> next -> next -> prev = head -> next;

head = delPos(head, 2);

printList(head);

return 0;

}

C

// C Program to delete node at a specific position // in Doubly Linked List

#include <stdio.h> #include <stdlib.h>

struct Node { int data; struct Node* prev; struct Node* next; };

// Function to delete a node at a specific position // in the doubly linked list struct Node* delPos(struct Node* head, int pos) {

// If the list is empty
if (head == NULL)
    return head;

struct Node* curr = head;

// Traverse to the node at the given position
for (int i = 1; curr != NULL && i < pos; ++i) {
    curr = curr->next;
}

// If the position is out of range
if (curr == NULL)
    return head;

// Update the previous node's next pointer
if (curr->prev != NULL)
    curr->prev->next = curr->next;

// Update the next node's prev pointer
if (curr->next != NULL)
    curr->next->prev = curr->prev;

// If the node to be deleted is the head node
if (head == curr)
    head = curr->next;

// Deallocate memory for the deleted node
free(curr);
return head;

}

void printList(struct Node* head) { struct Node* curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); }

struct Node* createNode(int d) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = d; new_node->prev = NULL; new_node->next = NULL; return new_node; }

int main() {

// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;

head = delPos(head, 2);

printList(head);

return 0;

}

Java

// Java Program to delete node at a specific position // in Doubly Linked List

class Node { int data; Node prev; Node next;

Node(int d) {
    data = d;
    prev = next = null;
}

}

public class GfG {

// Function to delete a node at a specific position 
// in the doubly linked list
static Node delPos(Node head, int pos) {

    // If the list is empty
    if (head == null)
        return head;

    Node curr = head;

    // Traverse to the node at the given position
    for (int i = 1; curr != null && i < pos; ++i) {
        curr = curr.next;
    }

    // If the position is out of range
    if (curr == null)
        return head;

    // Update the previous node's next pointer
    if (curr.prev != null)
        curr.prev.next = curr.next;

    // Update the next node's prev pointer
    if (curr.next != null)
        curr.next.prev = curr.prev;

    // If the node to be deleted is the head node
    if (head == curr)
        head = curr.next;

    // Deallocate memory for the deleted node
    curr = null;
    return head;
}

static void printList(Node head) {
    Node curr = head;
    while (curr != null) {
        System.out.print(curr.data + " ");
        curr = curr.next;
    }
    System.out.println();
}

public static void main(String[] args) {

    // Create a hardcoded doubly linked list:
    // 1 <-> 2 <-> 3
    Node head = new Node(1);
    head.next = new Node(2);
    head.next.prev = head;
    head.next.next = new Node(3);
    head.next.next.prev = head.next;

    head = delPos(head, 2);

    printList(head);
}

}

Python

Python Program to delete node at a specific position

in Doubly Linked List

class Node: def init(self, data): self.data = data self.prev = None self.next = None

Function to delete a node at a specific position

in the doubly linked list

def del_pos(head, pos):

# If the list is empty
if head is None:
    return head

curr = head

for i in range(1, pos):
    if curr is None:
        break
    curr = curr.next

if curr is None:
    return head

# Update the previous node's next pointer
if curr.prev is not None:
    curr.prev.next = curr.next

# Update the next node's prev pointer
if curr.next is not None:
    curr.next.prev = curr.prev

# If the node to be deleted is the head node
if head == curr:
    head = curr.next

# Deallocate memory for the deleted node
del curr
return head

def print_list(head): curr = head while curr is not None: print(curr.data, end=" ") curr = curr.next print()

if name == "main":

# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next

head = del_pos(head, 2)

print_list(head)

C#

// C# Program to delete node at a specific position // in Doubly Linked List

using System;

class Node { public int Data; public Node prev; public Node next;

public Node(int data) {
    Data = data;
    prev = null;
    next = null;
}

}

// Function to delete a node at a specific position // in the doubly linked list class GfG { static Node DelPos(Node head, int pos) {

    // If the list is empty
    if (head == null)
        return head;

    Node curr = head;

    // Traverse to the node at the given position
    for (int i = 1; curr != null && i < pos; ++i) {
        curr = curr.next;
    }

    // If the position is out of range
    if (curr == null)
        return head;

    // Update the previous node's next pointer
    if (curr.prev != null)
        curr.prev.next = curr.next;

    // Update the next node's prev pointer
    if (curr.next != null)
        curr.next.prev = curr.prev;

    // If the node to be deleted is the head node
    if (head == curr)
        head = curr.next;

    // Deallocate memory for the deleted node
    // In C#, the garbage collector handles this automatically
    return head;
}

static void PrintList(Node head) {
    Node curr = head;
    while (curr != null) {
        Console.Write(curr.Data + " ");
        curr = curr.next;
    }
    Console.WriteLine();
}

static void Main() {

    // Create a hardcoded doubly linked list:
    // 1 <-> 2 <-> 3
    Node head = new Node(1);
    head.next = new Node(2);
    head.next.prev = head;
    head.next.next = new Node(3);
    head.next.next.prev = head.next;

    head = DelPos(head, 2);

    PrintList(head);
}

}

JavaScript

// JavaScript Program to delete node at a specific position // in Doubly Linked List

class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } }

// Function to delete a node at a specific position // in the doubly linked list function delPos(head, pos) {

// If the list is empty
if (head === null)
    return head;

let curr = head;

// Traverse to the node at the given position
for (let i = 1; curr !== null && i < pos; ++i) {
    curr = curr.next;
}

// If the position is out of range
if (curr === null)
    return head;

// Update the previous node's next pointer
if (curr.prev !== null)
    curr.prev.next = curr.next;

// Update the next node's prev pointer
if (curr.next !== null)
    curr.next.prev = curr.prev;

// If the node to be deleted is the head node
if (head === curr)
    head = curr.next;

return head;

}

function printList(head) { let curr = head; while (curr !== null) { console.log(curr.data + " "); curr = curr.next; } console.log(); }

// Create a hardcoded doubly linked list: // 1 <-> 2 <-> 3 let head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next;

head = delPos(head, 2);

printList(head);

`

**Time Complexity: O(n), where **n is the number of nodes in the doubly linked list.
**Auxiliary Space: O(1)