Various operations on Fibonacci nodes in a Singly Linked list

Given a singly linked list containing N nodes, the task is to perform the following operations on the Fibonacci nodes present in it:

  1. Print all Fibonacci nodes present in the singly linked list.
  2. Find the total count of Fibonacci nodes present in the singly linked list.
  3. Find the minimum and the maximum Fibonacci nodes.
  4. Remove all the Fibonacci nodes from the singly linked list.

Examples:

Input: LL = 15 -> 16 -> 8 -> 6 -> 13
Output:
Fibonacci nodes = 8, 13
Number of Fibonacci nodes = 2
Minimum Fibonacci nodes in the list = 8
Maximum Fibonacci nodes in the list = 13
List after deleteting Fibonacci nodes = 15 -> 16 -> 6

Input: LL = 5 -> 3 -> 4 -> 2 -> 9
Output:
Fibonacci nodes = 5, 3, 2
Number of Fibonacci nodes = 3
Minimum Fibonacci nodes in the list = 2
Maximum Fibonacci nodes in the list = 5
List after deleteting Fibonacci nodes = 4 -> 9

Approach: The idea is to use hashing to precompute and store the Fibonacci nodes up to the maximum value in the Linked list, to make checking easy and efficient (in O(1) time).



  1. Traverse through the entire singly linked list and obtain the maximum value in the list.
  2. Now, build a hash table containing all the Fibonacci nodes less than or equal to the maximum value in the singly linked list.

After performing the above precomputation, we can check if a number is a Fibonacci or not in constant time. Therefore, in order to perform the above operations, the following approach is used:

  1. Print the Fibonacci nodes: Traverse through the linked list and check if the number is a Fibonacci value or not. If it is, then print it.
  2. Count the number of Fibonacci nodes: To count the number of Fibonacci nodes in the linked list, we traverse through the linked list and check if the number is a Fibonacci value or not.
  3. Find the minimum and the maximum Fibonacci nodes: Traverse through the linked list and check if the value at a node is a Fibonacci or not. If yes:
    • If the current node’s value is greater than max, then assign the current node’s value to max.
    • If the current node’s value is less than min, then assign the current node’s value to min.

    Finally, the max and the min variable contains the maximum and minimum Fibonacci nodes present in the linked list.

  4. Delete the Fibonacci nodes: In order to delete the Fibonacci values, traverse through the linked list and if the data is a Fibonacci, then delete the node containing the data using this approach.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to perform the
// operations on Fibonacci nodes
// of a Singly Linked list
  
#include <bits/stdc++.h>
using namespace std;
  
set<int> hashmap;
  
// Node of the singly linked list
struct Node {
    int data;
    Node* next;
};
  
// Function to insert a node at the beginning
// of the singly Linked List
void push(Node** head_ref, int new_data)
{
    // Allocate a new node
    Node* new_node = new Node;
  
    // Insert the data
    new_node->data = new_data;
    new_node->next = (*head_ref);
  
    // Move the head to point the new node
    (*head_ref) = new_node;
}
  
// Function to delete a node in a SLL
// head_ref --> pointer to head node pointer.
// del --> pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
    // Base case
    struct Node* temp = *head_ref;
  
    if (*head_ref == NULL || del == NULL)
        return;
  
    // If the node to be deleted is
    // the head node
    if (*head_ref == del)
        *head_ref = del->next;
  
    // Traverse list till not found
    // delete node
    while (temp->next != del) {
        temp = temp->next;
    }
  
    // Copy address of node
    temp->next = del->next;
  
    // Finally, free the memory
    // occupied by del
    free(del);
  
    return;
}
  
// Function that returns the largest element
// from the linked list.
int largestElement(struct Node* head_ref)
{
    // Declare a max variable and
    // initialize it with INT_MIN value.
    // INT_MIN is integer type and
    // its value is -32767 or less.
    int max = INT_MIN;
  
    Node* head = head_ref;
  
    // Loop to iterate the linked list
    while (head != NULL) {
  
        // If max is less than head->data then
        // assign value of head->data to max
        // otherwise node point to next node.
        if (max < head->data)
            max = head->data;
  
        head = head->next;
    }
    return max;
}
  
// Function to create a hash table
// to check Fibonacci nodes
void createHash(int maxElement)
{
    // Insert the first two
    // elements in the hash
    int prev = 0, curr = 1;
    hashmap.insert(prev);
    hashmap.insert(curr);
  
    // Add the elements until the max element
    // by using the previous two numbers
    while (curr <= maxElement) {
        int temp = curr + prev;
        hashmap.insert(temp);
        prev = curr;
        curr = temp;
    }
}
  
// Function to print the Fibonacci
// nodes in a linked list
int printFibonacci(Node** head_ref)
{
  
    int count = 0;
    Node* ptr = *head_ref;
  
    cout << "Fibonacci nodes = ";
  
    while (ptr != NULL) {
  
        // If current node is Fibonacci
        if (hashmap.find(ptr->data)
            != hashmap.end()) {
  
            // Update count
            cout << ptr->data << " ";
        }
  
        ptr = ptr->next;
    }
  
    cout << endl;
    return 0;
}
  
// Function to find the count of Fibonacci
// nodes in a linked list
int countFibonacci(Node** head_ref)
{
  
    int count = 0;
    Node* ptr = *head_ref;
  
    while (ptr != NULL) {
  
        // If current node is Fibonacci
        if (hashmap.find(ptr->data)
            != hashmap.end()) {
  
            // Update count
            count++;
        }
  
        ptr = ptr->next;
    }
  
    return count;
}
  
// Function to find maximum and minimum
// fibonacci nodes in a linked list
void minmaxFibonacciNodes(Node** head_ref)
{
    // Find the largest node value
    // in Singly Linked List
    int maxEle = largestElement(*head_ref);
  
    // Creating a set containing
    // all the Fibonacci nodes
    // upto the maximum data value
    // in the Singly Linked List
    // set<int> hash;
    // createHash(hash, maxEle);
  
    int minimum = INT_MAX;
    int maximum = INT_MIN;
    Node* ptr = *head_ref;
  
    while (ptr != NULL) {
  
        // If current node is fibonacci
        if (hashmap.find(ptr->data)
            != hashmap.end()) {
  
            // Update minimum
            minimum
                = min(minimum, ptr->data);
  
            // Update maximum
            maximum
                = max(maximum, ptr->data);
        }
        ptr = ptr->next;
    }
  
    cout << "Minimum Fibonacci node: "
         << minimum << endl;
    cout << "Maximum Fibonacci node: "
         << maximum << endl;
}
  
// Function to delete all the
// fibonacci nodes from the
// singly linked list
void deleteFibonacciNodes(Node** head_ref)
{
  
    Node* ptr = *head_ref;
    Node* next;
  
    // Iterating through the linked list
    while (ptr != NULL) {
        next = ptr->next;
  
        // If the node's data is fibonacci,
        // delete node 'ptr'
        if (hashmap.find(ptr->data) != hashmap.end())
            deleteNode(head_ref, ptr);
  
        ptr = next;
    }
}
  
// Function to print nodes in a
// given singly linked list
void printList(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
void operations(struct Node* head)
{
    // Find the largest node value
    // in Singly Linked List
    int maxEle = largestElement(head);
  
    // Creating a set containing
    // all Fibonacci nodes
    // upto the maximum data value
    // in the Singly Linked List
    createHash(maxEle);
  
    // Print all Fibonacci nodes
    printFibonacci(&head);
  
    // Count of Fibonacci nodes
    cout << "Count of Fibonacci nodes = "
         << countFibonacci(&head) << endl;
  
    // Minimum and maximum Fibonacci nodes
    minmaxFibonacciNodes(&head);
  
    // Delete Fibonacci nodes
    deleteFibonacciNodes(&head);
  
    cout << "List after deletion: ";
    printList(head);
}
  
// Driver program
int main()
{
    // Start with the empty list
    Node* head = NULL;
  
    // Create the linked list
    // 15 -> 16 -> 8 -> 6 -> 13
    push(&head, 13);
    push(&head, 6);
    push(&head, 8);
    push(&head, 16);
    push(&head, 15);
  
    operations(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to perform the
// operations on Fibonacci nodes
// of a Singly Linked list
   
  
import java.util.*;
  
class GFG{
   
static HashSet<Integer> hashmap = new HashSet<Integer>();
   
// Node of the singly linked list
static class Node {
    int data;
    Node next;
};
   
// Function to insert a node at the beginning
// of the singly Linked List
static Node push(Node head_ref, int new_data)
{
    // Allocate a new node
    Node new_node = new Node();
   
    // Insert the data
    new_node.data = new_data;
    new_node.next = head_ref;
   
    // Move the head to point the new node
    head_ref = new_node;
    return head_ref;
}
   
// Function to delete a node in a SLL
// head_ref -. pointer to head node pointer.
// del -. pointer to node to be deleted
static Node deleteNode(Node head_ref, Node del)
{
    // Base case
    Node temp = head_ref;
   
    if (head_ref == null || del == null)
        return null;
   
    // If the node to be deleted is
    // the head node
    if (head_ref == del)
        head_ref = del.next;
   
    // Traverse list till not found
    // delete node
    while (temp.next != del) {
        temp = temp.next;
    }
   
    // Copy address of node
    temp.next = del.next;
   
    // Finally, free the memory
    // occupied by del
    del = null;
   
    return head_ref;
}
   
// Function that returns the largest element
// from the linked list.
static int largestElement(Node head_ref)
{
    // Declare a max variable and
    // initialize it with Integer.MIN_VALUE value.
    // Integer.MIN_VALUE is integer type and
    // its value is -32767 or less.
    int max = Integer.MIN_VALUE;
   
    Node head = head_ref;
   
    // Loop to iterate the linked list
    while (head != null) {
   
        // If max is less than head.data then
        // assign value of head.data to max
        // otherwise node point to next node.
        if (max < head.data)
            max = head.data;
   
        head = head.next;
    }
    return max;
}
   
// Function to create a hash table
// to check Fibonacci nodes
static void createHash(int maxElement)
{
    // Insert the first two
    // elements in the hash
    int prev = 0, curr = 1;
    hashmap.add(prev);
    hashmap.add(curr);
   
    // Add the elements until the max element
    // by using the previous two numbers
    while (curr <= maxElement) {
        int temp = curr + prev;
        hashmap.add(temp);
        prev = curr;
        curr = temp;
    }
}
   
// Function to print the Fibonacci
// nodes in a linked list
static int printFibonacci(Node head_ref)
{
   
    int count = 0;
    Node ptr = head_ref;
   
    System.out.print("Fibonacci nodes = ");
   
    while (ptr != null) {
   
        // If current node is Fibonacci
        if (hashmap.contains(ptr.data)) {
   
            // Update count
            System.out.print(ptr.data+ " ");
        }
   
        ptr = ptr.next;
    }
   
    System.out.println();
    return 0;
}
   
// Function to find the count of Fibonacci
// nodes in a linked list
static int countFibonacci(Node head_ref)
{
   
    int count = 0;
    Node ptr = head_ref;
   
    while (ptr != null) {
   
        // If current node is Fibonacci
        if (hashmap.contains(ptr.data)) {
   
            // Update count
            count++;
        }
   
        ptr = ptr.next;
    }
   
    return count;
}
   
// Function to find maximum and minimum
// fibonacci nodes in a linked list
static void minmaxFibonacciNodes(Node head_ref)
{
    // Find the largest node value
    // in Singly Linked List
    int maxEle = largestElement(head_ref);
   
    // Creating a set containing
    // all the Fibonacci nodes
    // upto the maximum data value
    // in the Singly Linked List
    // HashSet<Integer> hash;
    // createHash(hash, maxEle);
   
    int minimum = Integer.MAX_VALUE;
    int maximum = Integer.MIN_VALUE;
    Node ptr = head_ref;
   
    while (ptr != null) {
   
        // If current node is fibonacci
        if (hashmap.contains(ptr.data)) {
   
            // Update minimum
            minimum
                = Math.min(minimum, ptr.data);
   
            // Update maximum
            maximum
                = Math.max(maximum, ptr.data);
        }
        ptr = ptr.next;
    }
   
    System.out.print("Minimum Fibonacci node: "
         + minimum +"\n");
    System.out.print("Maximum Fibonacci node: "
         + maximum +"\n");
}
   
// Function to delete all the
// fibonacci nodes from the
// singly linked list
static Node deleteFibonacciNodes(Node head_ref)
{
   
    Node ptr = head_ref;
    Node next;
   
    // Iterating through the linked list
    while (ptr != null) {
        next = ptr.next;
   
        // If the node's data is fibonacci,
        // delete node 'ptr'
        if (hashmap.contains(ptr.data))
            deleteNode(head_ref, ptr);
   
        ptr = next;
    }
    return head_ref;
}
   
// Function to print nodes in a
// given singly linked list
static void printList(Node head)
{
    while (head != null) {
        System.out.print(head.data+ " ");
        head = head.next;
    }
}
   
static void operations(Node head)
{
    // Find the largest node value
    // in Singly Linked List
    int maxEle = largestElement(head);
   
    // Creating a set containing
    // all Fibonacci nodes
    // upto the maximum data value
    // in the Singly Linked List
    createHash(maxEle);
   
    // Print all Fibonacci nodes
    printFibonacci(head);
   
    // Count of Fibonacci nodes
    System.out.print("Count of Fibonacci nodes = "
         + countFibonacci(head) +"\n");
   
    // Minimum and maximum Fibonacci nodes
    minmaxFibonacciNodes(head);
   
    // Delete Fibonacci nodes
    head = deleteFibonacciNodes(head);
   
    System.out.print("List after deletion: ");
    printList(head);
}
   
// Driver program
public static void main(String[] args)
{
    // Start with the empty list
    Node head = null;
   
    // Create the linked list
    // 15.16.8.6.13
    head = push(head, 13);
    head = push(head, 6);
    head = push(head, 8);
    head = push(head, 16);
    head = push(head, 15);
   
    operations(head);
   
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to perform the
// operations on Fibonacci nodes
// of a Singly Linked list
using System;
using System.Collections.Generic;
  
class GFG{
    
static HashSet<int> hashmap = new HashSet<int>();
    
// Node of the singly linked list
class Node {
    public int data;
    public Node next;
};
    
// Function to insert a node at the beginning
// of the singly Linked List
static Node push(Node head_ref, int new_data)
{
    // Allocate a new node
    Node new_node = new Node();
    
    // Insert the data
    new_node.data = new_data;
    new_node.next = head_ref;
    
    // Move the head to point the new node
    head_ref = new_node;
    return head_ref;
}
    
// Function to delete a node in a SLL
// head_ref -. pointer to head node pointer.
// del -. pointer to node to be deleted
static Node deleteNode(Node head_ref, Node del)
{
    // Base case
    Node temp = head_ref;
    
    if (head_ref == null || del == null)
        return null;
    
    // If the node to be deleted is
    // the head node
    if (head_ref == del)
        head_ref = del.next;
    
    // Traverse list till not found
    // delete node
    while (temp.next != del) {
        temp = temp.next;
    }
    
    // Copy address of node
    temp.next = del.next;
    
    // Finally, free the memory
    // occupied by del
    del = null;
    
    return head_ref;
}
    
// Function that returns the largest element
// from the linked list.
static int largestElement(Node head_ref)
{
    // Declare a max variable and
    // initialize it with int.MinValue value.
    // int.MinValue is integer type and
    // its value is -32767 or less.
    int max = int.MinValue;
    
    Node head = head_ref;
    
    // Loop to iterate the linked list
    while (head != null) {
    
        // If max is less than head.data then
        // assign value of head.data to max
        // otherwise node point to next node.
        if (max < head.data)
            max = head.data;
    
        head = head.next;
    }
    return max;
}
    
// Function to create a hash table
// to check Fibonacci nodes
static void createHash(int maxElement)
{
    // Insert the first two
    // elements in the hash
    int prev = 0, curr = 1;
    hashmap.Add(prev);
    hashmap.Add(curr);
    
    // Add the elements until the max element
    // by using the previous two numbers
    while (curr <= maxElement) {
        int temp = curr + prev;
        hashmap.Add(temp);
        prev = curr;
        curr = temp;
    }
}
    
// Function to print the Fibonacci
// nodes in a linked list
static int printFibonacci(Node head_ref)
{
    
    Node ptr = head_ref;
    
    Console.Write("Fibonacci nodes = ");
    
    while (ptr != null) {
    
        // If current node is Fibonacci
        if (hashmap.Contains(ptr.data)) {
    
            // Update count
            Console.Write(ptr.data+ " ");
        }
    
        ptr = ptr.next;
    }
    
    Console.WriteLine();
    return 0;
}
    
// Function to find the count of Fibonacci
// nodes in a linked list
static int countFibonacci(Node head_ref)
{
    
    int count = 0;
    Node ptr = head_ref;
    
    while (ptr != null) {
    
        // If current node is Fibonacci
        if (hashmap.Contains(ptr.data)) {
    
            // Update count
            count++;
        }
    
        ptr = ptr.next;
    }
    
    return count;
}
    
// Function to find maximum and minimum
// fibonacci nodes in a linked list
static void minmaxFibonacciNodes(Node head_ref)
{
    // Find the largest node value
    // in Singly Linked List
    int maxEle = largestElement(head_ref);
    
    // Creating a set containing
    // all the Fibonacci nodes
    // upto the maximum data value
    // in the Singly Linked List
    // HashSet<int> hash;
    // createHash(hash, maxEle);
    
    int minimum = int.MaxValue;
    int maximum = int.MinValue;
    Node ptr = head_ref;
    
    while (ptr != null) {
    
        // If current node is fibonacci
        if (hashmap.Contains(ptr.data)) {
    
            // Update minimum
            minimum
                = Math.Min(minimum, ptr.data);
    
            // Update maximum
            maximum
                = Math.Max(maximum, ptr.data);
        }
        ptr = ptr.next;
    }
    
    Console.Write("Minimum Fibonacci node: "
         + minimum +"\n");
    Console.Write("Maximum Fibonacci node: "
         + maximum +"\n");
}
    
// Function to delete all the
// fibonacci nodes from the
// singly linked list
static Node deleteFibonacciNodes(Node head_ref)
{
    
    Node ptr = head_ref;
    Node next;
    
    // Iterating through the linked list
    while (ptr != null) {
        next = ptr.next;
    
        // If the node's data is fibonacci,
        // delete node 'ptr'
        if (hashmap.Contains(ptr.data))
            deleteNode(head_ref, ptr);
    
        ptr = next;
    }
    return head_ref;
}
    
// Function to print nodes in a
// given singly linked list
static void printList(Node head)
{
    while (head != null) {
        Console.Write(head.data+ " ");
        head = head.next;
    }
}
    
static void operations(Node head)
{
    // Find the largest node value
    // in Singly Linked List
    int maxEle = largestElement(head);
    
    // Creating a set containing
    // all Fibonacci nodes
    // upto the maximum data value
    // in the Singly Linked List
    createHash(maxEle);
    
    // Print all Fibonacci nodes
    printFibonacci(head);
    
    // Count of Fibonacci nodes
    Console.Write("Count of Fibonacci nodes = "
         + countFibonacci(head) +"\n");
    
    // Minimum and maximum Fibonacci nodes
    minmaxFibonacciNodes(head);
    
    // Delete Fibonacci nodes
    head = deleteFibonacciNodes(head);
    
    Console.Write("List after deletion: ");
    printList(head);
}
    
// Driver program
public static void Main(String[] args)
{
    // Start with the empty list
    Node head = null;
    
    // Create the linked list
    // 15.16.8.6.13
    head = push(head, 13);
    head = push(head, 6);
    head = push(head, 8);
    head = push(head, 16);
    head = push(head, 15);
    
    operations(head);
    
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output:

Fibonacci nodes = 8 13 
Count of Fibonacci nodes = 2
Minimum Fibonacci node: 8
Maximum Fibonacci node: 13
List after deletion: 15 16 6

Time Complexity: O(N), where N is the number of nodes in the linked list.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji, princi singh