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
C++ Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer Given singly linked list with every node having an additional "arbitrary" pointer that currently points to NULL. Need to make the "arbitrary" pointer point to the next higher value node. We strongly recommend to minimize your browser and try this yourself first A Simple Solution is to traverse all n
5 min read
C++ Program For Printing Nth Node From The End Of A Linked List Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below list and n = 3, then output is "B" Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Use length
4 min read
C++ Program To Delete Nodes Which Have A Greater Value On Right Side Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: Input: 12->15->10->11->5->6->2->3->NULL Output: 15->11->6->3->NULL Explanation: 12, 10, 5 and 2 have been deleted because there is a greater value on the right
4 min read
C++ Program For Writing A Function To Get Nth Node In A Linked List Write a C++ Program to GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before m
4 min read
C++ Program For Removing Middle Points From a Linked List Of Line Segments Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked
4 min read
C++ Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15-
5 min read
C++ Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2->
5 min read
C++ Program For Sorting Linked List Which Is Already Sorted On Absolute Values Given a linked list that is sorted based on absolute values. Sort the list based on actual values.Examples: Input: 1 -> -10 Output: -10 -> 1 Input: 1 -> -2 -> -3 -> 4 -> -5 Output: -5 -> -3 -> -2 -> 1 -> 4 Input: -5 -> -10 Output: -10 -> -5 Input: 5 -> 10 Outpu
3 min read
C++ Program For Inserting A Node In A Linked List Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the listTable of ContentInsert a Node at the
9 min read
C++ Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves
3 min read