Point arbit pointer to greatest value right side node in a linked list | Link list node ; Function to reverse the linked list ; This function populates arbit pointer in every node to the greatest value to its right . ; Reverse given linked list ; Initialize pointer to maximum value node ; Traverse the reversed list ; Connect max through arbit pointer ; Update max if required ; Move ahead in reversed list ; Reverse modified linked list and return head . ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code
class Node { constructor ( val ) { this . data = val ; this . arbit = null ; this . next = null ; } } function reverse ( head ) { var prev = null , current = head , next = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } function populateArbit ( head ) { head = reverse ( head ) ; var max = head ; var temp = head . next ; while ( temp != null ) { temp . arbit = max ; if ( max . data < temp . data ) max = temp ; temp = temp . next ; } return reverse ( head ) ; } 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 ) ; head = populateArbit ( head ) ; document . write ( " " ) ; printNextArbitPointers ( head ) ;