Delete a Doubly Linked List node at a given position
Last Updated : 1 Sep, 2025
Given the head of a doubly linked list and an integer x, delete the node at the x-th position (1-based index) from the beginning of the list.
If x = 1, delete the head node and update the head pointer.
If x is greater than the length of the list, no deletion should be performed.
Ensure that the list remains valid after deletion, i.e., the prev and next pointers of the remaining nodes are correctly updated.
Input: x = 2
Output: 1 <-> 3 Explanation: The node at position 2 is 2. After deleting it, the remaining list is 1 <-> 3.
[Approach] Traversal and Pointer Adjustment - O(n) Time and O(1) Space
The idea is simple: find the node at the given position and remove it by updating the links of the nodes before and after it, so the list stays connected. If the node to delete is the head, we just move the head to the next node.
Step by Step Approach:
If the list is empty, there’s nothing to delete, so just return the head.
Go through the list to find the x-th node. If x is larger than the number of nodes, just return the head as it is.
If x is 1, we need to remove the head: move the head to the next node and make sure its previous link is null, then delete the old head.
For any other node, link its previous node to its next node, and if there’s a next node, link it back to the previous node, then remove the current node.
Return the head of the updated list.
Handling Deletion of Head and Last Node
Head Node: If the node to remove is the first one, we must move the head pointer to the next node. At the same time, if a next node exists, we should make sure its backward link no longer points to the old head.
Last Node: If the node to remove is the last one, we cut the link from its previous node so that the previous node becomes the new end of the list. In this case, the forward link is set to null.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*prev;Node*next;Node(intd){data=d;prev=next=NULL;}};Node*delPos(Node*head,intx){if(head==NULL)returnhead;Node*curr=head;// traverse to the node at the given positionfor(inti=1;curr!=NULL&&i<x;++i){curr=curr->next;}// position exceeds list length, no deletionif(curr==NULL)returnhead;// if the node to delete is not the first node// update previous node's nextif(curr->prev!=NULL)curr->prev->next=curr->next;// if the node to delete is not the last node// update next node's previf(curr->next!=NULL)curr->next->prev=curr->prev;// if deleting the head, move head pointer to next nodeif(head==curr)head=curr->next;deletecurr;returnhead;}// function to print the doubly linked listvoidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data;if(curr->next!=nullptr){cout<<" <-> ";}curr=curr->next;}cout<<endl;}intmain(){Node*head=newNode(1);head->next=newNode(2);head->next->prev=head;head->next->next=newNode(3);head->next->next->prev=head->next;head=delPos(head,2);printList(head);return0;}
Java
classNode{intdata;Nodeprev;Nodenext;Node(intd){data=d;prev=next=null;}}classGfG{publicstaticNodedelPos(Nodehead,intx){if(head==null){returnhead;}Nodecurr=head;// traverse to the node at the given positionfor(inti=1;curr!=null&&i<x;i++){curr=curr.next;}if(curr==null){// position exceeds list length, no deletionreturnhead;}// if the node to delete is not the first node// update previous node's nextif(curr.prev!=null)curr.prev.next=curr.next;// if the node to delete is not the last node// update next node's previf(curr.next!=null)curr.next.prev=curr.prev;// if deleting the head, move head pointer to next nodeif(head==curr)head=curr.next;curr=null;returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data);if(curr.next!=null){System.out.print(" <-> ");}curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){Nodehead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head=delPos(head,2);printList(head);}}
Python
classNode:def__init__(self,data):self.data=dataself.prev=Noneself.next=NonedefdelPos(head,x):ifheadisNone:returnheadcurr=head# traverse to the node at the given positionforiinrange(1,x):ifcurrisNone:# position exceeds list length, no deletionreturnheadcurr=curr.nextifcurrisNone:# position exceeds list length, no deletionreturnhead# if the node to delete is not the first node# update previous node's nextifcurr.previsnotNone:curr.prev.next=curr.next# if the node to delete is not the last node# update next node's previfcurr.nextisnotNone:curr.next.prev=curr.prev# if deleting the head, move head pointer # to next nodeifhead==curr:head=curr.nextdelcurr# free memory of the deleted nodereturnheaddefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end='')ifcurr.nextisnotNone:print(' <-> ',end='')curr=curr.nextprint()if__name__=="__main__":head=Node(1)head.next=Node(2)head.next.prev=headhead.next.next=Node(3)head.next.next.prev=head.nexthead=delPos(head,2)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodeprev;publicNodenext;publicNode(intd){data=d;prev=next=null;}}classGfG{staticNodedelPos(Nodehead,intx){if(head==null)returnhead;Nodecurr=head;// traverse to the node at the given positionfor(inti=1;curr!=null&&i<x;i++){curr=curr.next;}// position exceeds list length, no deletionif(curr==null)returnhead;// if the node to delete is not the first node// update previous node's nextif(curr.prev!=null)curr.prev.next=curr.next;// if the node to delete is not the last node// update next node's previf(curr.next!=null)curr.next.prev=curr.prev;// if deleting the head, move head pointer// to next nodeif(head==curr)head=curr.next;curr=null;returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data);if(curr.next!=null)Console.Write(" <-> ");curr=curr.next;}Console.WriteLine();}publicstaticvoidMain(string[]args){Nodehead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head=delPos(head,2);printList(head);}}
JavaScript
classNode{constructor(data){this.data=data;this.prev=null;this.next=null;}}functiondelPos(head,x){if(head===null)returnhead;letcurr=head;// traverse to the node at the given positionfor(leti=1;curr!==null&&i<x;i++){curr=curr.next;}// position exceeds list length, no deletionif(curr===null)returnhead;// if the node to delete is not the first node// update previous node's nextif(curr.prev!==null)curr.prev.next=curr.next;// if the node to delete is not the last node// update next node's previf(curr.next!==null)curr.next.prev=curr.prev;// if deleting the head, move head pointer to next nodeif(head===curr)head=curr.next;curr=null;returnhead;}functionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(curr.data.toString());if(curr.next!==null)process.stdout.write(" <-> ");curr=curr.next;}console.log();}// Driver Codelethead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head=delPos(head,2);printList(head);