Deletion at different positions in a Circular Linked List | A linked list node ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move first node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function print data of list ; if list is empty , simply show message ; traverse first to last node ; Function return number of nodes present in list ; if list is empty simply return length zero ; traverse forst to last node ; Function delete First node of Circular Linked List ; check list have any node if not then return ; check list have single node if yes then delete it and return ; traverse second to first ; now previous is last node and next is first node of list first node ( next ) link address put in last node ( previous ) link ; make second node as head node ; Function to delete last node of Circular Linked List ; check if list doesn 't have any node  if not then return ; check if list have single node if yes then delete it and return ; move first node to last previous ; Function delete node at a given poisition of Circular Linked List ; Find length of list ; check list have any node if not then return ; given index is in list or not ; delete first node ; traverse first to last node ; if index found delete that node ; Driver Code ; Deleting Node at position ; Deleting first Node ; Deleting last Node
class Node : NEW_LINE INDENT def __init__ ( self , new_data ) : NEW_LINE INDENT self . data = new_data NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT def Insert ( head , data ) : NEW_LINE INDENT current = head NEW_LINE newNode = Node ( 0 ) NEW_LINE if ( newNode == None ) : NEW_LINE INDENT print ( " Memory Error " ) NEW_LINE return None NEW_LINE DEDENT newNode . data = data NEW_LINE if ( head == None ) : NEW_LINE INDENT newNode . next = newNode NEW_LINE head = newNode NEW_LINE return head NEW_LINE DEDENT else : NEW_LINE INDENT while ( current . next != head ) : NEW_LINE INDENT current = current . next NEW_LINE DEDENT newNode . next = head NEW_LINE current . next = newNode NEW_LINE DEDENT return head NEW_LINE DEDENT def Display ( head ) : NEW_LINE INDENT current = head NEW_LINE if ( head == None ) : NEW_LINE INDENT print ( " Display List is empty " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT print ( current . data , end = " ▁ " ) NEW_LINE current = current . next NEW_LINE if ( current == head ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def Length ( head ) : NEW_LINE INDENT current = head NEW_LINE count = 0 NEW_LINE if ( head == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT current = current . next NEW_LINE count = count + 1 NEW_LINE if ( current == head ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def DeleteFirst ( head ) : NEW_LINE INDENT previous = head NEW_LINE next = head NEW_LINE if ( head == None ) : NEW_LINE INDENT print ( " List is empty " ) NEW_LINE return None NEW_LINE DEDENT if ( previous . next == previous ) : NEW_LINE INDENT head = None NEW_LINE return None NEW_LINE DEDENT while ( previous . next != head ) : NEW_LINE INDENT previous = previous . next NEW_LINE next = previous . next NEW_LINE DEDENT previous . next = next . next NEW_LINE head = previous . next NEW_LINE return head NEW_LINE DEDENT def DeleteLast ( head ) : NEW_LINE INDENT current = head NEW_LINE temp = head NEW_LINE previous = None NEW_LINE if ( head == None ) : NEW_LINE INDENT print ( " List is empty " ) NEW_LINE return None NEW_LINE DEDENT if ( current . next == current ) : NEW_LINE INDENT head = None NEW_LINE return None NEW_LINE DEDENT while ( current . next != head ) : NEW_LINE INDENT previous = current NEW_LINE current = current . next NEW_LINE DEDENT previous . next = current . next NEW_LINE head = previous . next NEW_LINE return head NEW_LINE DEDENT def DeleteAtPosition ( head , index ) : NEW_LINE INDENT len = Length ( head ) NEW_LINE count = 1 NEW_LINE previous = head NEW_LINE next = head NEW_LINE if ( head == None ) : NEW_LINE INDENT print ( " Delete Last List is empty " ) NEW_LINE return None NEW_LINE DEDENT if ( index >= len or index < 0 ) : NEW_LINE INDENT print ( " Index is not Found " ) NEW_LINE return None NEW_LINE DEDENT if ( index == 0 ) : NEW_LINE INDENT head = DeleteFirst ( head ) NEW_LINE return head NEW_LINE DEDENT while ( len > 0 ) : NEW_LINE INDENT if ( index == count ) : NEW_LINE INDENT previous . next = next . next NEW_LINE return head NEW_LINE DEDENT previous = previous . next NEW_LINE next = previous . next NEW_LINE len = len - 1 NEW_LINE count = count + 1 NEW_LINE DEDENT return head NEW_LINE DEDENT head = None NEW_LINE head = Insert ( head , 99 ) NEW_LINE head = Insert ( head , 11 ) NEW_LINE head = Insert ( head , 22 ) NEW_LINE head = Insert ( head , 33 ) NEW_LINE head = Insert ( head , 44 ) NEW_LINE head = Insert ( head , 55 ) NEW_LINE head = Insert ( head , 66 ) NEW_LINE print ( " Initial ▁ List : ▁ " ) NEW_LINE Display ( head ) NEW_LINE print ( " After Deleting node at index 4 :   " ) NEW_LINE head = DeleteAtPosition ( head , 4 ) NEW_LINE Display ( head ) NEW_LINE print ( " Initial List :   " ) NEW_LINE Display ( head ) NEW_LINE print ( " After Deleting first node :   " ) NEW_LINE head = DeleteFirst ( head ) NEW_LINE Display ( head ) NEW_LINE print ( " Initial List :   " ) NEW_LINE Display ( head ) NEW_LINE print ( " After Deleting last node :   " ) NEW_LINE head = DeleteLast ( head ) NEW_LINE Display ( head ) NEW_LINE