Delete the last node from doubly linked list
The problem revolves around the deletion of the last node from a doubly linked list. In a doubly linked list, each node holds a data element and two pointers, one pointing to the next node and the other pointing to the previous node. The objective is to remove the last node from the end of the list.
Problem Statement
Given a doubly linked list, the task is to delete the last node from the end of the list.
Example
Consider the following doubly linked list:
1 <-> 2 <-> 3 <-> 4 <-> -5 <-> 6 <-> 7 <-> 8
After deleting the last node, the list should be:
1 <-> 2 <-> 3 <-> 4 <-> -5 <-> 6 <-> 7
Idea to Solve
To address this problem, you need to find the last node in the doubly linked list and modify the pointers to remove it.
Pseudocode
function deleteLastNode():
if head is null:
print "Empty Linked List"
return
temp = head
// Find the last node
while temp's next is not null:
move temp to the next node
if temp equals head:
set head to null
else:
set temp's previous node's next pointer to null
Algorithm Explanation
- Start by initializing
temp
to thehead
. - Traverse the doubly linked list using the
temp
pointer:- Move
temp
to the next node until you reach the last node.
- Move
- After traversing, check if
temp
is equal to thehead
:- If it is, there's only one node in the list, so set
head
to null. - Otherwise, set the previous node's next pointer of
temp
to null.
- If it is, there's only one node in the list, so set
Program List
-
1) Delete last node in doubly linked list java
2) Delete last node in doubly linked list in c++
3) Delete last node in doubly linked list in c
4) Delete last node in doubly linked list in c#
5) Delete last node in doubly linked list in php
6) Delete last node in doubly linked list in node js
7) Delete last node in doubly linked list in python
8) Delete last node in doubly linked list in ruby
9) Delete last node in doubly linked list in scala
10) Delete last node in doubly linked list in swift
11) Delete last node in doubly linked list in kotlin
12) Delete last node in doubly linked list in vb.net
13) Delete last node in doubly linked list in golang
14) Delete last node in doubly linked list in typescript
Time Complexity
The time complexity of this algorithm is O(n), where n is the number of nodes in the doubly linked list. In the worst case, you might need to traverse the entire list to find the last node.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment