# 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 deleting 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 deleting 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, 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.
4. Delete the Fibonacci nodes: 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:

## Javascript



Output

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

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next