C++ Program For Pointing Arbit Pointer To Greatest Value Right Side Node In A Linked List
Last Updated :
03 May, 2023
Given singly linked list with every node having an additional “arbitrary” pointer that currently points to NULL. We need to make the “arbitrary” pointer to the greatest value node in a linked list on its right side.

A Simple Solution is to traverse all nodes one by one. For every node, find the node which has the greatest value on the right side and change the next pointer. The Time Complexity of this solution is O(n2).
An Efficient Solution can work in O(n) time. Below are the steps.
- Reverse the given linked list.
- Start traversing the linked list and store the maximum value node encountered so far. Make arbit of every node to point to max. If the data in the current node is more than the max node so far, update max.
- Reverse modified linked list and return head.
Following is the implementation of the above steps.
C++
// C++ program to point arbit pointers
// to highest value on its right
#include<bits/stdc++.h>
using namespace std;
// Link list node
struct Node
{
int data;
Node* next, *arbit;
};
/* Function to reverse the
linked list */
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;
}
// This function populates arbit pointer
// in every node to the greatest value
// to its right.
Node* populateArbit(Node *head)
{
// Reverse given linked list
head = reverse(head);
// Initialize pointer to maximum
// value node
Node *max = head;
// Traverse the reversed list
Node *temp = head->next;
while (temp != NULL)
{
// Connect max through arbit
// pointer
temp->arbit = max;
// Update max if required
if (max->data < temp->data)
max = temp;
// Move ahead in reversed list
temp = temp->next;
}
// Reverse modified linked list
// and return head.
return reverse(head);
}
// Utility function to print result
// linked list
void printNextArbitPointers(Node *node)
{
printf("Node Next Pointer Arbit Pointer");
while (node!=NULL)
{
cout << node->data <<
" ";
if (node->next)
cout << node->next->data <<
" ";
else cout << "NULL" << " ";
if (node->arbit)
cout << node->arbit->data;
else cout << "NULL";
cout << endl;
node = node->next;
}
}
/* Function to create a new node with
given data */
Node *newNode(int data)
{
Node *new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// Driver code
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: ");
printNextArbitPointers(head);
return 0;
}
Output:
Resultant Linked List is:
Node Next Pointer Arbit Pointer
5 10 10
10 2 3
2 3 3
3 NULL NULL
Time complexity: O(n)
Space complexity: O(1)
Recursive Solution:
We can recursively reach the last node and traverse the linked list from the end. The recursive solution doesn’t require reversing of the linked list. We can also use a stack in place of recursion to temporarily hold nodes. Thanks to Santosh Kumar Mishra for providing this solution.
C++
// C++ program to point arbit pointers
// to highest value on its right
#include <bits/stdc++.h>
using namespace std;
// Link list node
struct Node {
int data;
Node *next, *arbit;
};
// This function populates arbit pointer
// in every node to the greatest value
// to its right.
void populateArbit(Node* head)
{
// using static maxNode to keep track
// of maximum orbit node address on
// right side
static Node* maxNode;
// if head is null simply return
// the list
if (head == NULL)
return;
/* if head->next is null it means we
reached at the last node just update
the max and maxNode */
if (head->next == NULL) {
maxNode = head;
return;
}
/* Calling the populateArbit to the
next node */
populateArbit(head->next);
/* Updating the arbit node of the
current node with the maximum
value on the right side */
head->arbit = maxNode;
/* If current Node value id greater
then the previous right node then
update it */
if (head->data > maxNode->data)
maxNode = head;
return;
}
// Utility function to print result
// linked list
void printNextArbitPointers(Node* node)
{
printf("Node Next Pointer Arbit Pointer");
while (node != NULL) {
cout << node->data << " ";
if (node->next)
cout << node->next->data << " ";
else
cout << "NULL"
<< " ";
if (node->arbit)
cout << node->arbit->data;
else
cout << "NULL";
cout << endl;
node = node->next;
}
}
/* Function to create a new node
with given data */
Node* newNode(int data)
{
Node* new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// Driver code
int main()
{
Node* head = newNode(5);
head->next = newNode(10);
head->next->next = newNode(2);
head->next->next->next = newNode(3);
populateArbit(head);
printf("Resultant Linked List is: ");
printNextArbitPointers(head);
return 0;
}
Output:
Resultant Linked List is:
Node Next Pointer Arbit Pointer
5 10 10
10 2 3
2 3 3
3 NULL NULL
Time complexity: O(n) where n is no of nodes in a linked list.
Auxiliary Space: O(1) since using constant space for variables
Please refer complete article on Point arbit pointer to greatest value right side node in a linked list for more details!
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read