Modify Linked List by replacing each node by nearest multiple of K | 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 ; Given Linked List
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 ) ;