Modify Linked List by replacing each node by nearest multiple of K | Java program for the above approach ; Structure of node ; Function to replace the node N by the nearest multiple of K ; Traverse the Linked List ; If data is less than K ; If the data of current node is same as K ; Otherwise change the value ; Move to the next node ; Return the updated LL ; Function to print the nodes of the Linked List ; Traverse the LL ; Print the node 's data ; Driver Code ; Given Linked List
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 ) ; } }