Remove all Fibonacci Nodes from a Circular Singly Linked List | Java program to delete all the Fibonacci nodes from the circular singly linked list ; Structure for a node ; Function to insert 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 null 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 del ; Function to find the maximum node of the circular linked list ; Pointer for traversing ; Initialize head to the current pointer ; Initialize min int value to max ; While the last node is not reached ; If current node data is greater for max then replace it ; Function to create hash table to check Fibonacci numbers ; Adding the first two elements to the hash ; Inserting the Fibonacci numbers into the hash ; Function to delete all the Fibonacci nodes from the singly circular linked list ; Find the largest node value in Circular Linked List ; Creating a hash 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
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static void deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; del = null ; return ; } static int largestElement ( Node head_ref ) { Node current ; current = head_ref ; int maxEle = Integer . MIN_VALUE ; do { if ( current . data > maxEle ) { maxEle = current . data ; } current = current . next ; } while ( current != head_ref ) ; return maxEle ; } static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static void deleteFibonacciNodes ( Node head ) { int maxEle = largestElement ( head ) ; HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , maxEle ) ; Node ptr = head ; Node next ; do { if ( hash . contains ( ptr . data ) ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 34 ) ; head = push ( head , 11 ) ; head = push ( head , 9 ) ; deleteFibonacciNodes ( head ) ; printList ( head ) ; } }