Remove all Fibonacci Nodes from a Circular Singly Linked List | Structure for a node ; Function to add a node at the beginning of a Circular linked list ; Create a new node and make head as next of it . ; If linked list is not None then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by delt ; Function to find the maximum node of the circular linked list ; Pointer for traversing ; Initialize head to the current pointer ; Initialize min value to max ; While the last node is not reached ; If current node data is greater for max then replace it ; Function to create hashmap table to check Fibonacci numbers ; Adding the first two elements to the hashmap ; Inserting the Fibonacci numbers into the hashmap ; Function to delete all the Fibonacci nodes from the singly circular linked list ; Find the largest node value in Circular Linked List ; Creating a hashmap containing all the Fibonacci numbers upto the maximum data value in the circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ is ▁ Fibonacci , ▁ ▁ delete ▁ node ▁ ' ptr ; Point to the next node ; Function to print nodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 9.11 .34 .6 .13 .20
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , data ) : NEW_LINE INDENT ptr1 = Node ( ) NEW_LINE temp = head_ref ; NEW_LINE ptr1 . data = data ; NEW_LINE ptr1 . next = head_ref ; NEW_LINE if ( head_ref != None ) : NEW_LINE INDENT while ( temp . next != head_ref ) : NEW_LINE INDENT temp = temp . next ; NEW_LINE DEDENT temp . next = ptr1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ptr1 . next = ptr1 ; NEW_LINE DEDENT head_ref = ptr1 ; NEW_LINE return head_ref NEW_LINE DEDENT def deleteNode ( head_ref , delt ) : NEW_LINE INDENT temp = head_ref ; NEW_LINE if ( head_ref == delt ) : NEW_LINE INDENT head_ref = delt . next ; NEW_LINE DEDENT while ( temp . next != delt ) : NEW_LINE INDENT temp = temp . next ; NEW_LINE DEDENT temp . next = delt . next ; NEW_LINE del ( delt ) ; NEW_LINE return ; NEW_LINE DEDENT def largestElement ( head_ref ) : NEW_LINE INDENT current = None NEW_LINE current = head_ref ; NEW_LINE maxEle = - 10000000 NEW_LINE while ( True ) : NEW_LINE INDENT if ( current . data > maxEle ) : NEW_LINE INDENT maxEle = current . data ; NEW_LINE DEDENT current = current . next ; NEW_LINE if ( current == head_ref ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return maxEle ; NEW_LINE DEDENT def createHash ( hashmap , maxElement ) : NEW_LINE INDENT prev = 0 NEW_LINE curr = 1 ; NEW_LINE hashmap . add ( prev ) ; NEW_LINE hashmap . add ( curr ) ; NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev ; NEW_LINE hashmap . add ( temp ) ; NEW_LINE prev = curr ; NEW_LINE curr = temp ; NEW_LINE DEDENT DEDENT def deleteFibonacciNodes ( head ) : NEW_LINE INDENT maxEle = largestElement ( head ) ; NEW_LINE hashmap = set ( ) NEW_LINE createHash ( hashmap , maxEle ) ; NEW_LINE ptr = head ; NEW_LINE next = None NEW_LINE while ( True ) : NEW_LINE INDENT if ( ptr . data in hashmap ) : NEW_LINE INDENT deleteNode ( head , ptr ) ; NEW_LINE DEDENT next = ptr . next ; NEW_LINE ptr = next ; NEW_LINE if ( ptr == head ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT def printList ( head ) : NEW_LINE INDENT temp = head ; NEW_LINE if ( head != None ) : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT print ( temp . data , end = ' ▁ ' ) NEW_LINE temp = temp . next NEW_LINE if ( temp == head ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None ; NEW_LINE head = push ( head , 20 ) ; NEW_LINE head = push ( head , 13 ) ; NEW_LINE head = push ( head , 6 ) ; NEW_LINE head = push ( head , 34 ) ; NEW_LINE head = push ( head , 11 ) ; NEW_LINE head = push ( head , 9 ) ; NEW_LINE deleteFibonacciNodes ( head ) ; NEW_LINE printList ( head ) ; NEW_LINE DEDENT