VOOZH about

URL: https://www.geeksforgeeks.org/dsa/deletion-at-beginning-removal-of-first-node-in-a-doubly-linked-list/

⇱ Deletion at beginning (Removal of first node) in a Doubly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deletion at beginning (Removal of first node) in a Doubly Linked List

Last Updated : 9 Aug, 2024

Given a doubly linked list, the task is to delete the node from the beginning of the linked list.

Examples:

Input : 1 <-> 2 <-> 3 -> NULL
Output : 2 <-> 3 <-> NULL

Input : 2 <-> 4 <-> 6 <-> 8 <-> 33 <-> 67 <-> NULL
Output : 4 <-> 6 <-> 8 <-> 33 <-> 67 <-> NULL

Approach:

The idea is to update the head of the doubly linked list to the node next to head node and if the new head is not NULL, then set the previous pointer of new head to NULL.

👁 Deletion-at-the-Beginning-of-Doubly-Linked-List
Deletion at the beginning of Doubly Linked List

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

  • Check if the list is empty, there is nothing to delete, return.
  • Store the head pointer in a variable, say temp.
  • Update the head of linked list to the node next to the current head, head = head->next.
  • If the new head is not NULL, update the previous pointer of new head to NULL, head->prev = NULL.

Output
Original Linked List: 1 2 3 
After Deletion at the beginning: 2 3 

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags: