Write an function to delete an element from a doubly link list
Question
Write an function to delete an element from a doubly link list
Solution
Sure, here is a Python function that deletes a node from a doubly linked list:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
self.prev = None
class DoublyLinkedList:
def __init__(self):
self.head = None
def delete_node(self, key):
cur = self.head
while cur:
if cur.data == key and cur == self.head:
# Case 1: if node to be deleted is head node
if not cur.next:
cur = None
self.head = None
return
else:
nxt = cur.next
cur.next = None
nxt.prev = None
cur = None
self.head = nxt
return
elif cur.data == key:
# Case 2: if node to be deleted is not head node
if cur.next:
nxt = cur.next
prev = cur.prev
prev.next = nxt
nxt.prev = prev
cur.next = None
cur.prev = None
cur = None
return
else:
prev = cur.prev
prev.next = None
cur.prev = None
cur = None
return
cur = cur.next
This function traverses the doubly linked list until it finds the node with the data matching the key. It then checks if the node to be deleted is the head node or not. If it is the head node, it checks if there is a next node. If there is, it sets the next node's prev pointer to None and deletes the current node. If there isn't a next node, it simply deletes the current node. If the node to be deleted is not the head node, it checks if there is a next node. If there is, it sets the next node's prev pointer to the current node's prev pointer, sets the prev node's next pointer to the current node's next pointer, and deletes the current node. If there isn't a next node, it sets the prev node's next pointer to None and deletes the current node.
Similar Questions
Given a doubly Linked list and a position. The task is to delete a node from a given position (position starts from 1) in a doubly linked list and return the head of the doubly Linked list.
Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value.ExampleAfter removing the node at position , .Function DescriptionComplete the deleteNode function in the editor below.deleteNode has the following parameters:- SinglyLinkedListNode pointer llist: a reference to the head node in the list- int position: the position of the node to removeReturns- SinglyLinkedListNode pointer: a reference to the head of the modified listInput FormatThe first line of input contains an integer , the number of elements in the linked list.Each of the next lines contains an integer, the node data values in order.The last line contains an integer, , the position of the node to delete.Constraints, where is the element of the linked list.Sample Input8206219741593Sample Output20 6 2 7 4 15 9ExplanationThe original list is . After deleting the node at position , the list is .
What is doubly Linked List? What are its applications?Explain how an element can be deleted from doublylinked list using C program
Write a Java function that delete odd nodes from the doubly linked list.
Delete all nodes from a doubly linked list that are divisible by K
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.