Point arbit pointer to greatest value right side node in a linked list | Link list node ; This function populates arbit pointer in every node to the greatest value to its right . ; if head is null simply return the list ; if head -> next is null it means we reached at the last node just update the max and maxNode ; Calling the populateArbit to the next node ; updating the arbit node of the current node with the maximum value on the right side ; if current Node value id greater then the previous right node then update it ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . arbit = null ; this . next = null ; } } var maxNode ; function populateArbit ( head ) { if ( head == null ) return ; if ( head . next == null ) { maxNode = head ; return ; } populateArbit ( head . next ) ; head . arbit = maxNode ; if ( head . data > maxNode . data ) maxNode = head ; return ; } function printNextArbitPointers ( node ) { document . write ( " " ) ; while ( node != null ) { document . write ( node . data + " " ) ; if ( node . next != null ) document . write ( node . next . data + " " ) ; else document . write ( " " + " " ) ; if ( node . arbit != null ) document . write ( node . arbit . data ) ; else document . write ( " " ) ; document . write ( " " ) ; node = node . next ; } } function newNode ( data ) { var new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } var head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; populateArbit ( head ) ; document . write ( " " ) ; printNextArbitPointers ( head ) ;