Python program to delete a node from the end of the Circular Linked List

Created with Sketch.

Python program to delete a node from the end of the Circular Linked List

In this program, we will create a circular linked list and delete a node from the end of the list. If the list is empty, it will display the message “List is empty”. If the list is not empty, we will loop through the list till second last node is reached. We will make second last node as the new tail, and this new tail will point to head and delete the previous tail.

Python program to delete a node from the end of the Circular Linked List

Circular linked list after deleting node from end

Python program to delete a node from the end of the Circular Linked List

Here, in the above list, D is the last node which needs to be deleted. We will iterate through the list till C. Make C as new tail and C will point back to head A.

ALGORITHM:

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list, and it has two nodes: head and tail. It has two methods: deleteEnd() and display() .
  3. deleteEnd() will delete the node from the end 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, iterate through the loop till current.next!= tail.
  • Now, current will point to the node previous to tail. Make current as new tail and tail will point to head thus, deletes the node from last.

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 will points to head again.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represents the node of list.  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.next = None;
  6. class CreateList:
  7.     #Declaring head and tail pointer as null.  
  8.     def __init__(self):
  9.         self.head = Node(None);
  10.         self.tail = Node(None);
  11.         self.head.next = self.tail;
  12.         self.tail.next = self.head;
  13.     #This function will add the new node at the end of the list.  
  14.     def add(self,data):
  15.         newNode = Node(data);
  16.         #Checks if the list is empty.  
  17.         if self.head.data is None:
  18.             #If list is empty, both head and tail would point to new node.  
  19.             self.head = newNode;
  20.             self.tail = newNode;
  21.             newNode.next = self.head;
  22.         else:
  23.             #tail will point to new node.  
  24.             self.tail.next = newNode;
  25.             #New node will become new tail.  
  26.             self.tail = newNode;
  27.             #Since, it is circular linked list tail will point to head.  
  28.             self.tail.next = self.head;
  29.     #Deletes node from end of the list  
  30.     def deleteEnd(self):
  31.         #Checks whether list is empty  
  32.         if(self.head == None):
  33.             return;
  34.         else:
  35.             #Checks whether contain only one element  
  36.             if(self.head != self.tail ):
  37.                 current = self.head;
  38.                 #Loop will iterate till the second last element as current.next is pointing to tail  
  39.                 while(current.next != self.tail):
  40.                     current = current.next;
  41.                 #Second last element will be new tail  
  42.                 self.tail = current;
  43.                 #Tail will point to head as it is a circular linked list  
  44.                 self.tail.next = self.head;
  45.             #If the list contains only one element   
  46.             #Then it will remove it and both head and tail will point to null  
  47.             else:
  48.                 self.head = self.tail = None;
  49.     #Displays all the nodes in the list  
  50.     def display(self):
  51.         current = self.head;
  52.         if self.head is None:
  53.             print(“List is empty”);
  54.             return;
  55.         else:
  56.             #Prints each node by incrementing pointer.  
  57.             print(current.data),
  58.             while(current.next != self.head):
  59.                 current = current.next;
  60.                 print(current.data),
  61.             print(“\n”);
  62. class CircularLinkedList:
  63.     cl = CreateList();
  64.     #Adds data to the list  
  65.     cl.add(1);
  66.     cl.add(2);
  67.     cl.add(3);
  68.     cl.add(4);
  69.     #Printing original list  
  70.     print(“Original List:”);
  71.     cl.display();
  72.     while(cl.head != None):
  73.         cl.deleteEnd();
  74.         #Printing updated list  
  75.         print(“Updated List:”);
  76.         cl.display();

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *