VOOZH about

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

⇱ Deletion at end (Removal of last node) in a Doubly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deletion at end (Removal of last node) in a Doubly Linked List

Last Updated : 9 Aug, 2024

Given a doubly linked list, the task is to delete the last node of the given linked list.

Examples:

Input: 1 <-> 2 <-> 3 <-> NULL
Output: 1 <-> 2 <-> NULL
Explanation: The last node of the linked list is 3, so 3 is deleted.

Input: 15 -> NULL
Output: NULL
Explanation: The last node of the linked list is 15, so 15 is deleted.

Approach:

To perform the deletion operation at the end of doubly linked list, we need to traverse the list to find thesecond last node, then set its next pointer tonull.

👁 Deletion-at-the-End-in-Doubly-Linked-List
Deletion at the End of Doubly Linked List

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

  • Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
  • If the list is not empty, then move to the last node of the doubly linked list, say curr.
  • Update the second-to-last node’s next pointer to NULL, curr->prev->next = NULL.
  • Free the memory allocated for the node that was deleted.

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

Time Complexity: O(N), where N is the number of nodes in the linked list
Auxiliary Space: O(1)

Comment
Article Tags: