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* 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 assigns the correct node address to each node’s
// arbit pointer
Node* solve(Node *head)
{
    // first reverse the given list
    head = reverse(head);
 
    // Initialize a pointer with first node of 
    // reversed list
    Node *max_node = head;
 
    // Initialize a pointer to traverse the list
    Node *curr = head->next;
    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
    return reverse(head);
}

// this function will print value of each node's
// next node and arbit node
void checkValue(Node *head)
{
    cout<<"node\tNext Pointer\tArbit Pointer\n";
    while (head!=NULL)
    {
        cout << head->data << "\t\t";
 
        if (head->next)
            cout << head->next->data << "\t\t";
        else cout << "NULL" << "\t\t";
 
        if (head->arbit)
            cout << head->arbit->data;
        else cout << "NULL";
 
        cout << endl;
        head = head->next;
    }
}

// main function
int main()
{
    Node *head = new Node(7);
    head->next = new Node(2);
    head->next->next = new Node(12);
    head->next->next->next = new Node(1);
    head->next->next->next->next = new Node(8);
    
    head = solve(head);
    checkValue(head);

    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).

Previous post Types of Linked list
Next post C program to Reverse a Linked List

Leave a Reply

Your email address will not be published. Required fields are marked *