Modifique la lista enlazada reemplazando cada Node por el múltiplo más cercano de K

Dada una sola Lista Vinculada L que consta de N Nodes y un número entero K , la tarea es modificar el valor de cada Node de la Lista Vinculada dada a su múltiplo más cercano de K , sin exceder el valor del Node.

Ejemplos:

Entrada: LL: 1 -> 2 -> 3 -> 5, K = 2 
Salida: 0 -> 2 -> 2 -> 4
Explicación:
El LL resultante tiene valores menores que el valor LL original y es un múltiplo de K (= 2).

Entrada: LL:13 -> 14 -> 26 -> 29 -> 40, K = 13
Salida: 13 -> 13 -> 26 -> 26 -> 39

Enfoque: el problema dado se puede resolver recorriendo la lista enlazada dada y para cada Node recorrido, encuentre el valor mínimo, digamos val , del valor del Node dividido por K y actualice los valores del Node a val*K .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#include <iostream>
using namespace std;
 
// Structure of node
struct node {
    int data;
    node* next;
};
 
// Function to replace the node N
// by the nearest multiple of K
node* EvalNearestMult(node* N, int K)
{
    node* temp = N;
    int t;
 
    // Traverse the Linked List
    while (N != NULL) {
 
        // If data is less than K
        if (N->data < K)
            N->data = 0;
 
        else {
 
            // If the data of current
            // node is same as K
            if (N->data == K)
                N->data = K;
 
            // Otherwise change the value
            else {
                N->data = (N->data / K) * K;
            }
        }
 
        // Move to the next node
        N = N->next;
    }
 
    // Return the updated LL
    return temp;
}
 
// Function to print the nodes of
// the Linked List
void printList(node* N)
{
    // Traverse the LL
    while (N != NULL) {
 
        // Print the node's data
        cout << N->data << "  ";
        N = N->next;
    }
}
 
// Driver Code
int main()
{
    // Given Linked List
    node* head = NULL;
    node* second = NULL;
    node* third = NULL;
 
    head = new node();
    second = new node();
    third = new node();
 
    head->data = 3;
    head->next = second;
    second->data = 4;
    second->next = third;
    third->data = 8;
    third->next = NULL;
 
    node* t = EvalNearestMult(head, 3);
 
    printList(t);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Structure of node
    static class node {
        int data;
        node next;
 
        node(int d)
        {
            data = d;
        }
    }
 
    // Function to replace the node N
    // by the nearest multiple of K
    static node EvalNearestMult(node N, int K)
    {
        node temp = N;
        int t;
 
        // Traverse the Linked List
        while (N != null) {
 
            // If data is less than K
            if (N.data < K)
                N.data = 0;
 
            else {
 
                // If the data of current
                // node is same as K
                if (N.data == K)
                    N.data = K;
 
                // Otherwise change the value
                else {
                    N.data = (N.data / K) * K;
                }
            }
 
            // Move to the next node
            N = N.next;
        }
 
        // Return the updated LL
        return temp;
    }
 
    // Function to print the nodes of
    // the Linked List
    static void printList(node N)
    {
        // Traverse the LL
        while (N != null) {
 
            // Print the node's data
            System.out.print(N.data + "  ");
            N = N.next;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Linked List
        node head = new node(3);
        head.next = new node(5);
          head.next.next = new node(8);
 
        node t = EvalNearestMult(head, 3);
 
        printList(t);
    }
}
 
// This code is contributed by Dharanendra L V

Python3

# Python program for the above approach
 
# Structure of Node
class Node:
    def __init__(self, data):
        self.data = data;
        self.next = None;
     
# Function to replace the Node N
# by the nearest multiple of K
def EvalNearestMult(N, K):
    temp = N;
    t;
 
    # Traverse the Linked List
    while (N != None):
 
        # If data is less than K
        if (N.data < K):
            N.data = 0;
 
        else:
 
            # If the data of current
            # Node is same as K
            if (N.data == K):
                N.data = K;
 
            # Otherwise change the value
            else:
                N.data = (N.data // K) * K;
             
         
 
        # Move to the next Node
        N = N.next;
     
 
    # Return the updated LL
    return temp;
 
# Function to print Nodes of
# the Linked List
def printList(N):
   
    # Traverse the LL
    while (N != None):
 
        # Print Node's data
        print(N.data , end="  ");
        N = N.next;
     
# Driver Code
if __name__ == '__main__':
   
    # Given Linked List
    head = Node(3);
    head.next = Node(5);
    head.next.next = Node(8);
    t = None;
    t = EvalNearestMult(head, 3);
 
    printList(t);
 
# This code is contributed by gauravrajput1

C#

// C# program for the above approach
using System;
 
public class GFG
{
 
  // Structure of node
  public    class node {
    public    int data;
    public    node next;
 
    public    node(int d) {
      data = d;
    }
  }
 
  // Function to replace the node N
  // by the nearest multiple of K
  static node EvalNearestMult(node N, int K) {
    node temp = N;
 
    // Traverse the Linked List
    while (N != null) {
 
      // If data is less than K
      if (N.data < K)
        N.data = 0;
 
      else {
 
        // If the data of current
        // node is same as K
        if (N.data == K)
          N.data = K;
 
        // Otherwise change the value
        else {
          N.data = (N.data / K) * K;
        }
      }
 
      // Move to the next node
      N = N.next;
    }
 
    // Return the updated LL
    return temp;
  }
 
  // Function to print the nodes of
  // the Linked List
  static void printList(node N) {
    // Traverse the LL
    while (N != null) {
 
      // Print the node's data
      Console.Write(N.data + "  ");
      N = N.next;
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Given Linked List
    node head = new node(3);
    head.next = new node(5);
    head.next.next = new node(8);
 
    node t = EvalNearestMult(head, 3);
 
    printList(t);
  }
}
 
// This code is contributed by gauravrajput1

Javascript

<script>
// Javascript program for the above approach
 
// Structure of node
class node {
     
    constructor()
    {
        this.data = 0;
        this.next = null;
    }
};
 
// Function to replace the node N
// by the nearest multiple of K
function EvalNearestMult(N, K)
{
    var temp = N;
    var t;
 
    // Traverse the Linked List
    while (N != null) {
 
        // If data is less than K
        if (N.data < K)
            N.data = 0;
 
        else {
 
            // If the data of current
            // node is same as K
            if (N.data == K)
                N.data = K;
 
            // Otherwise change the value
            else {
                N.data = parseInt(N.data / K) * K;
            }
        }
 
        // Move to the next node
        N = N.next;
    }
 
    // Return the updated LL
    return temp;
}
 
// Function to print the nodes of
// the Linked List
function printList(N)
{
    // Traverse the LL
    while (N != null) {
 
        // Print the node's data
        document.write( N.data + "  ");
        N = N.next;
    }
}
 
// Driver Code
// Given Linked List
var head = null;
var second = null;
var third = null;
head = new node();
second = new node();
third = new node();
head.data = 3;
head.next = second;
second.data = 4;
second.next = third;
third.data = 8;
third.next = null;
var t = EvalNearestMult(head, 3);
printList(t);
 
// This code is contributed by rrrtnx.
 
</script>
Producción: 

3  3  6

 

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por prithvirajtiwari28 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *