Modify Linked List by replacing each node by nearest multiple of K

Given a singly Linked List L consisting of N nodes and an integer K, the task is to modify the value of each node of the given Linked List to its nearest multiple of K, not exceeding the value of the node.
Examples:
Input: LL: 1 -> 2 -> 3 -> 5, K = 2Â
Output: 0 -> 2 -> 2 -> 4
Explanation:
The resultant LL has values having less than the original LL value and is a multiple of K(= 2).Input: LL:13 -> 14 -> 26 -> 29 -> 40, K = 13
Output: 13 -> 13 -> 26 -> 26 -> 39
Approach: The given problem can be solved by traversing the given Linked List and for each node traversed, find the floor value, say val, of the node value divided by K and update the node values to val*K.
Below is the implementation of the above approach:
C++
// C++ program for the above approachÂ
#include <iostream>using namespace std;Â
// Structure of nodestruct 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 Listvoid printList(node* N){Â Â Â Â // Traverse the LLÂ Â Â Â while (N != NULL) {Â
        // Print the node's data        cout << N->data << " ";        N = N->next;    }}Â
// Driver Codeint 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 approachimport 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 Nodeclass Node: Â Â Â Â def __init__(self, data):Â Â Â Â Â Â Â Â self.data = data; Â Â Â Â Â Â Â Â self.next = None;Â Â Â Â Â # Function to replace the Node N# by the nearest multiple of Kdef 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 Listdef printList(N):Â Â Â Â Â Â Â # Traverse the LLÂ Â Â Â while (N != None):Â
        # Print Node's data        print(N.data , end=" ");        N = N.next;     # Driver Codeif __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 approachusing 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 nodeclass 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 Listfunction printList(N){Â Â Â Â // Traverse the LLÂ Â Â Â while (N != null) {Â
        // Print the node's data        document.write( N.data + " ");        N = N.next;    }}Â
// Driver Code// Given Linked Listvar 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> |
3 3 6
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



