Remove all nodes from a Doubly Linked List containing Fibonacci numbers

Given a doubly linked list containing N nodes, the task is to remove all the nodes from the list which contains Fibonacci numbers.

Examples:

Input: DLL = 15 <=> 16 <=> 8 <=> 7 <=> 13
Output: 15 <=> 16 <=> 7
Explanation:
The linked list contains two fibonacci numbers 8 and 13.
Hence, these nodes have been deleted.

Input: DLL = 5 <=> 3 <=> 4 <=> 2 <=> 9
Output: 4 <=> 9
Explanation:
The linked list contains three fibonacci numbers 5, 3 and 2.
Hence, these nodes have been deleted.

Approach: The idea is to use hashing to store and check the Fibonacci numbers.



  1. Traverse through the entire doubly linked list and obtain the maximum value in the list.
  2. Now, in order to check for the Fibonacci numbers, build a hash table containing all the Fibonacci numbers less than or equal to the maximum value in the linked list.
  3. Finally, traverse the nodes of the doubly linked list one by one and remove the nodes which contains Fibonacci numbers as their data value.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to delete all
// Fibonacci nodes from the
// doubly linked list
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Node of the doubly linked list
struct Node {
    int data;
    Node *prev, *next;
};
  
// Function to insert a node at the beginning
// of the Doubly Linked List
void push(Node** head_ref, int new_data)
{
    // Allocate the node
    Node* new_node
        = (Node*)malloc(sizeof(struct Node));
  
    // Insert the data
    new_node->data = new_data;
  
    // Since we are adding at the beginning,
    // prev is always NULL
    new_node->prev = NULL;
  
    // Link the old list off the new node
    new_node->next = (*head_ref);
  
    // Change the prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
  
    // Move the head to point to the new node
    (*head_ref) = new_node;
}
  
// Function to find the largest
// nodes in the Doubly Linked List
int LargestInDLL(struct Node** head_ref)
{
    struct Node *max, *temp;
  
    // Initialize two-pointer temp
    // and max on the head node
    temp = max = *head_ref;
  
    // Traverse the whole doubly linked list
    while (temp != NULL) {
  
        // If temp's data is greater than
        // the max's data, then max = temp
        // and return max->data
        if (temp->data > max->data)
            max = temp;
  
        temp = temp->next;
    }
    return max->data;
}
  
// Function to create hash table to
// check Fibonacci numbers
void createHash(set<int>& hash, int maxElement)
{
    int prev = 0, curr = 1;
    hash.insert(prev);
    hash.insert(curr);
  
    // Inserting the Fibonacci numbers
    // until the maximum element in the
    // Linked List
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.insert(temp);
        prev = curr;
        curr = temp;
    }
}
  
// Function to delete a node
// in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del --> pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
    // Base case
    if (*head_ref == NULL || del == NULL)
        return;
  
    // If the node to be deleted is head node
    if (*head_ref == del)
        *head_ref = del->next;
  
    // Change next only if node to be
    // deleted is not the last node
    if (del->next != NULL)
        del->next->prev = del->prev;
  
    // Change prev only if node to be
    // deleted is not the first node
    if (del->prev != NULL)
        del->prev->next = del->next;
  
    // Finally, free the memory
    // occupied by del
    free(del);
  
    return;
}
  
// Function to delete all fibonacci nodes
// from the doubly linked list
void deleteFibonacciNodes(Node** head_ref)
{
    // Find the largest node value
    // in Doubly Linked List
    int maxEle = LargestInDLL(head_ref);
  
    // Creating a set containing
    // all the fibonacci numbers
    // upto the maximum data value
    // in the Doubly Linked List
    set<int> hash;
    createHash(hash, maxEle);
  
    Node* ptr = *head_ref;
    Node* next;
  
    // Iterating through the linked list
    while (ptr != NULL) {
        next = ptr->next;
  
        // If node's data is fibonacci,
        // delete node 'ptr'
        if (hash.find(ptr->data) != hash.end())
            deleteNode(head_ref, ptr);
  
        ptr = next;
    }
}
  
// Function to print nodes in a
// given doubly linked list
void printList(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver program
int main()
{
  
    Node* head = NULL;
  
    // Create the doubly linked list
    // 15 <-> 16 <-> 8 <-> 6 <-> 13
    push(&head, 13);
    push(&head, 6);
    push(&head, 8);
    push(&head, 16);
    push(&head, 15);
  
    cout << "Original List: ";
    printList(head);
  
    deleteFibonacciNodes(&head);
  
    cout << "\nModified List: ";
    printList(head);
}

chevron_right


Output:

Original List: 15 16 8 6 13 
Modified List: 15 16 6

Time Complexity: O(N), where N is the total number of nodes.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.