# Point arbit pointer to greatest value right-side node in a linked list

### Introduciton

The linked list is one of the most important concepts to know while preparing for interviews. Having a good grasp of a linked list can be a huge plus point in coding interviews.

### Problem Statement

Given a linked list in which each node will have an extra arbitrary pointer pointing to NULL initially. We need to make that arbitrary pointer point to the greatest valued node on the right side of that node.

### Problem Statement Understanding

The problem statement is quite straightforward; we will be given a singly linked list in which, apart from the next pointer, we will also have an arbit pointer which will be NULL initially, and we need to make that arbit pointer point to the greatest value in the right part of the list.

Let's try to understand the problem with the help of examples.

Let the input given to us be: • For each node, we need to find the biggest valued node in its right and attach its arbit pointer to the biggest node.
• For nodes ‘7’ and ‘2’, the biggest value in their right is node ‘12’ so, we will make the arbit pointer of both the nodes point to node with value ‘12’.
• Similarly, for nodes ‘12’ and ‘1’, the biggest value in their right is node ‘8’ so, we will make the arbit pointer of both the nodes point to node with value ‘8’.
• And for node ‘8’, there is no node on its right side. So, its arbit pointer remains NULL.

So, the output will be: Taking another example, if the given linked list is: • Now, in this case, for ‘5’ the biggest valued node on the right side of ‘5’ is the node with value ‘9’, so we will make arbit pointer of node ‘5’ point to node with value ‘9’.
• Similarly, for each node, we will make their arbit pointer point to the greatest valued node on their right side. Arbit of ‘9’ will point to ‘8’, arbit of ‘7’ will point to ‘8’, arbit of ‘8’ wil point to ‘6’ and arbit of ‘6’ will point to NULL as there are no nodes on right size of the node with value ‘6’.

So, our output will be: Now, I think from the above examples, the problem statement is clear. Let’s see how we can approach it.

Before moving to the approach section, try to think about how you can approach this problem.

• If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.

Let’s move to the approach section.

### Approach 1

Our approach will be simple:

• We will keep two iterators for iterating the list.
• Once we fix the first iterator, we will move the second iterator from first iterators next till the end of the list to find the maximum valued node.
• After that, we will connect the arbit pointer of the node pointed by the first iterator to the maximum valued node found by the second iterator.
• We repeat the same process for each node.

Time Complexity: O(n2), n is the number of nodes in the list.
Space Complexity: O(1), no extra space used.

The above approach seems trivial but, it has a huge time complexity of O(n2). Can we reduce that and make our code more efficient in terms of time?

• The answer is yes, and let us see the way to do that in the below approach.

Let’s move to a more optimized approach.

### Approach 2

• Every time, to find the greatest valued node corresponding to a node, we need to look right in the list (iterate the list from node's next to the end of the list), to make things simpler, we can think of iterating the list from end to start.
• We cannot iterate the list backward so, we need to reverse the list first, if we want to do so.
• Now, after reversing the list, we only need to keep the address of the max-valued node in a variable and update it on each iteration.
• On reaching each node, we will have a max valued node in its left, stored in a variable, and we just need to connect the node’s arbit pointer to the stored node.
• Finally, after connecting the arbit pointers of each node of the list, we need to reverse the list again to keep it to its original form.

### Algorithm

• First, reverse the given linked list.
• Initialize a pointer max_node with the first node in the reversed list.
• Initialize a pointer curr with the second node in the reversed list.
• Run a while loop till curr is not NULL.
• Update arbit pointer of curr node with max_node.
• If curr data is greater than max_node data, then update max_node with curr.
• Advance curr by one node.
• Again reverse the list and return it.

### Dry Run  ### Code Implementation

```#include
using namespace std;

// class with constructor for a node of Linked list
class Node {
public:
int data;
Node* next,*arbit;
// constructor
Node(int x){
data = x;
next = NULL;
arbit = NULL;
}
};

// This function reverses the given list and returns
// the head of the reversed list
{
Node *prev = NULL, *current = head, *next;
while (current != NULL)
{
next  = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}

// This function assigns the correct node address to each node’s
// arbit pointer
{
// first reverse the given list

// Initialize a pointer with first node of
// reversed list

// Initialize a pointer to traverse the list
while (curr != NULL)
{
// connect the curr pointer with max_node
// pointer which will have the maximum valued
// node's value in left
curr->arbit = max_node;

// If this node is greater than max_node
// then update the max_node
if (max_node->data < curr->data)
max_node = curr;

// advance curr to move to next node
curr = curr->next;
}

// again reverse the list to bring it again
// to its original form
}

// this function will print value of each node's
// next node and arbit node
{
cout<<"node\tNext Pointer\tArbit Pointer\n";
{

else cout << "NULL" << "\t\t";

else cout << "NULL";

cout << endl;
}
}

// main function
int main()
{

return 0;
}
```

#### Output

node Next Pointer Arbit Pointer
7 2 12
2 12 12
12 1 8
1 8 8
8 NULL NULL

Time Complexity: O(n), where n is the total number of nodes in the list

So, in this blog, we have tried to explain how you can Point arbit pointer of each node in a given list to the greatest value right-side node in the most optimal way. This is a good problem to strengthen your concepts in LinkedList and if you want to practice more such problems, you can checkout Prepbytes (Linked List).