VOOZH about

URL: https://www.geeksforgeeks.org/dsa/replace-every-node-in-linked-list-with-its-closest-fibonacci-number/

⇱ Replace every node in linked list with its closest fibonacci number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replace every node in linked list with its closest fibonacci number

Last Updated : 23 Jul, 2025

Given a singly linked list of integers, the task is to replace every node with its closest Fibonacci number and return the modified linked list.

Examples:

Input: List: 3->5->9->12->13->15->null
Output: 3->5->8->13->13->13->null
Explanation: The closest Fibonacci numbers for each node are:

  • Node 1 (value = 3): Closest Fibonacci number = 3.
  • Node 2 (value = 5): Closest Fibonacci number = 5.
  • Node 3 (value = 9): Closest Fibonacci number = 8.
  • Node 4 (value = 12): Closest Fibonacci number = 13.
  • Node 5 (value = 13): Closest Fibonacci number = 13.
  • Node 6 (value = 15): Closest Fibonacci number = 13.

Input: List : 1->2->3->4->5->null
Output: 1->2->3->5->5->null
Explanation: The closest Fibonacci numbers for each node are:

  • Node 1 (value = 1): Closest Fibonacci number = 1.
  • Node 2 (value = 2): Closest Fibonacci number = 2.
  • Node 3 (value = 3): Closest Fibonacci number = 3.
  • Node 4 (value = 4): Closest Fibonacci number = 5.
  • Node 5 (value = 5): Closest Fibonacci number = 5.

Approach: This can be solved with the following idea:

This problem requires us to modify a linked list such that each node is replaced with its closest Fibonacci number. To solve this problem, we can start by creating an array of Fibonacci numbers that are greater than the maximum value in the linked list. We can then iterate over each node in the linked list, finding the closest Fibonacci number using the array we created.

Below are the steps for the above idea:

  • Define a function getClosestFibonacci() that takes an integer n and a vector fib as input.
  • Define an integer variable i and set it to 0.
  • While the ith Fibonacci number in fib is less than n, increment i.
  • If the ith Fibonacci number in fib is equal to n or if i is 0, return n.
  • Otherwise, if n is closer to the i-1th Fibonacci number than the ith Fibonacci number, return the i-1th Fibonacci number. Otherwise, return the ith Fibonacci number.
  • Define a function replaceWithClosestFibonacci() that takes a linked list head as input.
  • Create a vector fib that contains all the Fibonacci numbers up to a certain limit. We can start by adding 0 and 1 to the vector, and then use a loop to generate additional Fibonacci numbers until we reach a limit (e.g., 1000000).
  • Define a pointer curr and set it to head.
  • While curr is not null, set the value of curr to the closest Fibonacci number using getClosestFibonacci() and the fib vector, and move to curr to the next node in the linked list.
  • Return the modified linked list head.

Below is the implementation of the above approach:


Output
3->5->8->13->13->13->null

Time Complexity: O(n*logn)
Auxiliary Space: O(k)

Comment