# Python program to delete a new node from the beginning of the doubly linked list.

## Python program to delete a new node from the beginning of the doubly linked list.

In this program, we will create a doubly linked list and delete a node from the beginning of the list. If the list is empty, print the message “List is empty”. If the list is not empty, we will make the head to point to the next node in the list then; we will delete the first node.

Consider the above example, new was the head of the list. Make head to point to next node in the list. Now, node 1 will become the new head of the list thus, deleting node new.

### ALGORITHM:

1. Define a Node class which represents a node in the list. It will have three properties: data, previous which will point to the previous node and next which will point to the next node.
2. 2Define another class for creating a doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
3. 3deleteFromStart() will delete a node from the beginning of the list:
• It first checks whether the head is null (empty list) then, it will return from the function as there is no node present in the list.
• If the list is not empty, it will check whether the list has only one node.
• If the list has only one node, it will set both head and tail to null.
• If the list has more than one node then, the head will point to the next node in the list and delete the old head node.

a. display() will show all the nodes present in the list.

• Define a new node ‘current’ that will point to the head.
• Print current.data till current points to null.
• Current will point to the next node in the list in each iteration.

### PROGRAM:

1. #Represent a node of doubly linked list
2. class Node:
3.     def __init__(self,data):
4.         self.data = data;
5.         self.previous = None;
6.         self.next = None;
7. class DeleteStart:
9.     def __init__(self):
11.         self.tail = None;
14.         #Create a new node
15.         newNode = Node(data);
16.         #If list is empty
18.             #Both head and tail will point to newNode
19.             self.head = self.tail = newNode;
20.             #head’s previous will point to None
22.             #tail’s next will point to None, as it is the last node of the list
23.             self.tail.next = None;
24.         else:
25.             #newNode will be added after tail such that tail’s next will point to newNode
26.             self.tail.next = newNode;
27.             #newNode’s previous will point to tail
28.             newNode.previous = self.tail;
29.             #newNode will become new tail
30.             self.tail = newNode;
31.             #As it is last node, tail’s next will point to None
32.             self.tail.next = None;
33.     #deleteFromStart() will delete a node from the beginning of the list
34.     def deleteFromStart(self):
35.         #Checks whether list is empty
37.             return;
38.         else:
39.             #Checks whether the list contains only one element
41.                 #head will point to next node in the list
45.             #If the list contains only one element
46.             #then, it will remove the node, and now both head and tail will point to None
47.             else:
48.                 self.head = self.tail = None;
49.     #display() will print out the nodes of the list
50.     def display(self):
51.         #Node current will point to head
54.             print(“List is empty”);
55.             return;
56.         while(current != None):
57.             #Prints each node by incrementing pointer.
58.             print(current.data),
59.             current = current.next;
60.         print();
61. dList = DeleteStart();
62. #Add nodes to the list
68. #Printing original list
69. print(“Original List: “);
70. dList.display();
72.     dList.deleteFromStart();
73.     #Printing updated list
74.     print(“Updated List: “);
75.     dList.display();

Output:

```Original List:
1 2 3 4 5
Updated List:
2 3 4 5
Updated List:
3 4 5
Updated List:
4 5
Updated List:
5
Updated List:
List is empty```