Replace every node in linked list with its closest fibonacci number
Last Updated :
05 Sep, 2023
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:
C++
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int val;
Node* next;
Node( int v)
: val(v), next(nullptr)
{
}
};
int getClosestFibonacci( int n, vector< int >& fib)
{
int i = 0;
while (fib[i] < n) {
i++;
}
if (fib[i] == n || i == 0) {
return n;
}
else {
return (n - fib[i - 1] < fib[i] - n) ? fib[i - 1]
: fib[i];
}
}
Node* replaceWithClosestFibonacci(Node* head)
{
vector< int > fib;
fib.push_back(0);
fib.push_back(1);
int fibNum = 1;
while (fibNum <= 1000000) {
fib.push_back(fibNum);
fibNum = fib[fib.size() - 1] + fib[fib.size() - 2];
}
Node* curr = head;
while (curr != nullptr) {
curr->val = getClosestFibonacci(curr->val, fib);
curr = curr->next;
}
return head;
}
void printList(Node* head)
{
Node* curr = head;
while (curr != nullptr) {
cout << curr->val << "->" ;
curr = curr->next;
}
cout << "null" << endl;
}
int main()
{
Node* head = new Node(3);
head->next = new Node(5);
head->next->next = new Node(9);
head->next->next->next = new Node(12);
head->next->next->next->next = new Node(13);
head->next->next->next->next->next = new Node(15);
head = replaceWithClosestFibonacci(head);
printList(head);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class Node {
int val;
Node next;
Node( int v) {
val = v;
next = null ;
}
}
public class Main {
static int getClosestFibonacci( int n, List<Integer> fib) {
int i = 0 ;
while (fib.get(i) < n) {
i++;
}
if (fib.get(i) == n || i == 0 ) {
return n;
} else {
return (n - fib.get(i - 1 ) < fib.get(i) - n) ?
fib.get(i - 1 ) : fib.get(i);
}
}
static Node replaceWithClosestFibonacci(Node head) {
List<Integer> fib = new ArrayList<>();
fib.add( 0 );
fib.add( 1 );
int fibNum = 1 ;
while (fibNum <= 1000000 ) {
fib.add(fibNum);
fibNum = fib.get(fib.size() - 1 ) +
fib.get(fib.size() - 2 );
}
Node curr = head;
while (curr != null ) {
curr.val = getClosestFibonacci(curr.val, fib);
curr = curr.next;
}
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null ) {
System.out.print(curr.val + "->" );
curr = curr.next;
}
System.out.println( "null" );
}
public static void main(String[] args) {
Node head = new Node( 3 );
head.next = new Node( 5 );
head.next.next = new Node( 9 );
head.next.next.next = new Node( 12 );
head.next.next.next.next = new Node( 13 );
head.next.next.next.next.next = new Node( 15 );
head = replaceWithClosestFibonacci(head);
printList(head);
}
}
|
Python3
import math
class Node:
def __init__( self , v):
self .val = v
self . next = None
def getClosestFibonacci(n, fib):
i = 0
while fib[i] < n:
i + = 1
if fib[i] = = n or i = = 0 :
return n
else :
return fib[i - 1 ] if (n - fib[i - 1 ] < fib[i] - n) else fib[i]
def replaceWithClosestFibonacci(head):
fib = [ 0 , 1 ]
fibNum = 1
while fibNum < = 1000000 :
fib.append(fibNum)
fibNum = fib[ - 1 ] + fib[ - 2 ]
curr = head
while curr ! = None :
curr.val = getClosestFibonacci(curr.val, fib)
curr = curr. next
return head
def printList(head):
curr = head
while curr ! = None :
print (curr.val, end = "->" )
curr = curr. next
print ( "null" )
if __name__ = = '__main__' :
head = Node( 3 )
head. next = Node( 5 )
head. next . next = Node( 9 )
head. next . next . next = Node( 12 )
head. next . next . next . next = Node( 13 )
head. next . next . next . next . next = Node( 15 )
head = replaceWithClosestFibonacci(head)
printList(head)
|
C#
using System;
using System.Collections.Generic;
public class Node
{
public int val;
public Node next;
public Node( int v)
{
val = v;
next = null ;
}
}
public class GFG
{
public static int GetClosestFibonacci( int n, List< int > fib)
{
int i = 0;
while (fib[i] < n)
{
i++;
}
if (fib[i] == n || i == 0)
{
return n;
}
else
{
return (n - fib[i - 1] < fib[i] - n) ? fib[i - 1] : fib[i];
}
}
public static Node ReplaceWithClosestFibonacci(Node head)
{
List< int > fib = new List< int > { 0, 1 };
int fibNum = 1;
while (fibNum <= 1000000)
{
fib.Add(fibNum);
fibNum = fib[fib.Count - 1] + fib[fib.Count - 2];
}
Node curr = head;
while (curr != null )
{
curr.val = GetClosestFibonacci(curr.val, fib);
curr = curr.next;
}
return head;
}
public static void PrintList(Node head)
{
Node curr = head;
while (curr != null )
{
Console.Write(curr.val + "->" );
curr = curr.next;
}
Console.WriteLine( "null" );
}
public static void Main()
{
Node head = new Node(3);
head.next = new Node(5);
head.next.next = new Node(9);
head.next.next.next = new Node(12);
head.next.next.next.next = new Node(13);
head.next.next.next.next.next = new Node(15);
head = ReplaceWithClosestFibonacci(head);
PrintList(head);
}
}
|
Javascript
class Node {
constructor(v) {
this .val = v;
this .next = null ;
}
}
function getClosestFibonacci(n, fib) {
let i = 0;
while (fib[i] < n) {
i++;
}
if (fib[i] === n || i === 0) {
return n;
} else {
return (n - fib[i - 1] < fib[i] - n) ?
fib[i - 1] : fib[i];
}
}
function replaceWithClosestFibonacci(head) {
const fib = [0, 1];
let fibNum = 1;
while (fibNum <= 1000000) {
fib.push(fibNum);
fibNum = fib[fib.length - 1] +
fib[fib.length - 2];
}
let curr = head;
while (curr !== null ) {
curr.val = getClosestFibonacci(curr.val, fib);
curr = curr.next;
}
return head;
}
function printList(head) {
let curr = head;
while (curr !== null ) {
console.log(curr.val + "->" );
curr = curr.next;
}
console.log( "null" );
}
const head = new Node(3);
head.next = new Node(5);
head.next.next = new Node(9);
head.next.next.next = new Node(12);
head.next.next.next.next = new Node(13);
head.next.next.next.next.next = new Node(15);
replaceWithClosestFibonacci(head);
printList(head);
|
Output
3->5->8->13->13->13->null
Time Complexity: O(n*logn)
Auxiliary Space: O(k)
Share your thoughts in the comments
Please Login to comment...