Point arbit pointer to greatest value right side node in a linked list | Node class ; 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 : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE self . arbit = None NEW_LINE DEDENT DEDENT def reverse ( head ) : NEW_LINE INDENT prev = None NEW_LINE current = head NEW_LINE next = None NEW_LINE while ( current != None ) : NEW_LINE INDENT next = current . next NEW_LINE current . next = prev NEW_LINE prev = current NEW_LINE current = next NEW_LINE DEDENT return prev NEW_LINE DEDENT def populateArbit ( head ) : NEW_LINE INDENT head = reverse ( head ) NEW_LINE max = head NEW_LINE temp = head . next NEW_LINE while ( temp != None ) : NEW_LINE INDENT temp . arbit = max NEW_LINE if ( max . data < temp . data ) : NEW_LINE INDENT max = temp NEW_LINE DEDENT temp = temp . next NEW_LINE DEDENT return reverse ( head ) NEW_LINE DEDENT def printNextArbitPointers ( node ) : NEW_LINE INDENT print ( " Node Next Pointer Arbit Pointer " ) NEW_LINE while ( node != None ) : NEW_LINE INDENT print ( node . data , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE if ( node . next != None ) : NEW_LINE INDENT print ( node . next . data , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " , " TABSYMBOL TABSYMBOL " , end = " " ) NEW_LINE DEDENT if ( node . arbit != None ) : NEW_LINE INDENT print ( node . arbit . data , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT new_node = Node ( 0 ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT head = newNode ( 5 ) NEW_LINE head . next = newNode ( 10 ) NEW_LINE head . next . next = newNode ( 2 ) NEW_LINE head . next . next . next = newNode ( 3 ) NEW_LINE head = populateArbit ( head ) NEW_LINE print ( " Resultant Linked List is : " ) NEW_LINE printNextArbitPointers ( head ) NEW_LINE