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++
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
node* EvalNearestMult(node* N, int K)
{
node* temp = N;
int t;
while (N != NULL) {
if (N->data < K)
N->data = 0;
else {
if (N->data == K)
N->data = K;
else {
N->data = (N->data / K) * K;
}
}
N = N->next;
}
return temp;
}
void printList(node* N)
{
while (N != NULL) {
cout << N->data << " " ;
N = N->next;
}
}
int main()
{
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
import java.io.*;
class GFG {
static class node {
int data;
node next;
node( int d)
{
data = d;
}
}
static node EvalNearestMult(node N, int K)
{
node temp = N;
int t;
while (N != null ) {
if (N.data < K)
N.data = 0 ;
else {
if (N.data == K)
N.data = K;
else {
N.data = (N.data / K) * K;
}
}
N = N.next;
}
return temp;
}
static void printList(node N)
{
while (N != null ) {
System.out.print(N.data + " " );
N = N.next;
}
}
public static void main(String[] args)
{
node head = new node( 3 );
head.next = new node( 5 );
head.next.next = new node( 8 );
node t = EvalNearestMult(head, 3 );
printList(t);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self . next = None ;
def EvalNearestMult(N, K):
temp = N;
t;
while (N ! = None ):
if (N.data < K):
N.data = 0 ;
else :
if (N.data = = K):
N.data = K;
else :
N.data = (N.data / / K) * K;
N = N. next ;
return temp;
def printList(N):
while (N ! = None ):
print (N.data , end = " " );
N = N. next ;
if __name__ = = '__main__' :
head = Node( 3 );
head. next = Node( 5 );
head. next . next = Node( 8 );
t = None ;
t = EvalNearestMult(head, 3 );
printList(t);
|
C#
using System;
public class GFG
{
public class node {
public int data;
public node next;
public node( int d) {
data = d;
}
}
static node EvalNearestMult(node N, int K) {
node temp = N;
while (N != null ) {
if (N.data < K)
N.data = 0;
else {
if (N.data == K)
N.data = K;
else {
N.data = (N.data / K) * K;
}
}
N = N.next;
}
return temp;
}
static void printList(node N) {
while (N != null ) {
Console.Write(N.data + " " );
N = N.next;
}
}
public static void Main(String[] args)
{
node head = new node(3);
head.next = new node(5);
head.next.next = new node(8);
node t = EvalNearestMult(head, 3);
printList(t);
}
}
|
Javascript
<script>
class node {
constructor()
{
this .data = 0;
this .next = null ;
}
};
function EvalNearestMult(N, K)
{
var temp = N;
var t;
while (N != null ) {
if (N.data < K)
N.data = 0;
else {
if (N.data == K)
N.data = K;
else {
N.data = parseInt(N.data / K) * K;
}
}
N = N.next;
}
return temp;
}
function printList(N)
{
while (N != null ) {
document.write( N.data + " " );
N = N.next;
}
}
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
08 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...