Point arbit pointer to greatest value right side node in a linked list | C ++ program to point arbit pointers to highest value on its right ; 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 program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next , * arbit ; } ; Node * reverse ( Node * head ) { Node * prev = NULL , * current = head , * next ; while ( current != NULL ) { next = current -> next ; current -> next = prev ; prev = current ; current = next ; } return prev ; } Node * populateArbit ( Node * head ) { head = reverse ( head ) ; Node * max = head ; Node * temp = head -> next ; while ( temp != NULL ) { temp -> arbit = max ; if ( max -> data < temp -> data ) max = temp ; temp = temp -> next ; } return reverse ( head ) ; } void printNextArbitPointers ( Node * node ) { printf ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer STRNEWLINE " ) ; while ( node != NULL ) { cout << node -> data << " TABSYMBOL TABSYMBOL " ; if ( node -> next ) cout << node -> next -> data << " TABSYMBOL TABSYMBOL " ; else cout << " NULL " << " TABSYMBOL TABSYMBOL " ; if ( node -> arbit ) cout << node -> arbit -> data ; else cout << " NULL " ; cout << endl ; node = node -> next ; } } Node * newNode ( int data ) { Node * new_node = new Node ; new_node -> data = data ; new_node -> next = NULL ; return new_node ; } int main ( ) { Node * head = newNode ( 5 ) ; head -> next = newNode ( 10 ) ; head -> next -> next = newNode ( 2 ) ; head -> next -> next -> next = newNode ( 3 ) ; head = populateArbit ( head ) ; printf ( " Resultant ▁ Linked ▁ List ▁ is : ▁ STRNEWLINE " ) ; printNextArbitPointers ( head ) ; return 0 ; }