{"id":27587,"date":"2018-04-04T19:34:04","date_gmt":"2018-04-04T14:04:04","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27587"},"modified":"2018-04-04T19:34:04","modified_gmt":"2018-04-04T14:04:04","slug":"cpp-algorithm-point-arbit-pointer-to-greatest-value-right-side-node-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cpp-algorithm-point-arbit-pointer-to-greatest-value-right-side-node-in-a-linked-list\/","title":{"rendered":"Cpp Algorithm-Point arbit pointer to greatest value right side node in a linked list"},"content":{"rendered":"<p>Given singly linked list with every node having an additional \u201carbitrary\u201d pointer that currently points to NULL. We need to make the \u201carbitrary\u201d pointer to greatest value node in a linked list on its right side.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-27604\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Point-arbit-pointer-to-greatest-value-right-side-node-in-a-linked-list.png\" alt=\"Point arbit pointer to greatest value right side node in a linked list\" width=\"638\" height=\"253\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Point-arbit-pointer-to-greatest-value-right-side-node-in-a-linked-list.png 638w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Point-arbit-pointer-to-greatest-value-right-side-node-in-a-linked-list-300x119.png 300w\" sizes=\"(max-width: 638px) 100vw, 638px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>A <strong>Simple Solution<\/strong> is to traverse all nodes one by one. For every node, find the node which has greatest value on right side and change the next pointer. Time Complexity of this solution is O(n<sup>2<\/sup>).<\/p>\n<p>An <strong>Efficient Solution<\/strong> can work in O(n) time. Below are steps.<\/p>\n<ol>\n<li>Reverse given linked list.<\/li>\n<li>Start traversing linked list and store maximum value node encountered so far. Make arbit of every node to point to max. If the data in current node is more than max node so far, update max.<\/li>\n<li>Reverse modified linked list and return head.<\/li>\n<\/ol>\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to point arbit pointers to highest<br\/>\/\/ value on its right<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/* Link list node *\/<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    Node* next, *arbit;<br\/>};<br\/> <br\/>\/* Function to reverse the linked list *\/<br\/>Node* reverse(Node *head)<br\/>{<br\/>    Node *prev = NULL, *current = head, *next;<br\/>    while (current != NULL)<br\/>    {<br\/>        next  = current-&gt;next;<br\/>        current-&gt;next = prev;<br\/>        prev = current;<br\/>        current = next;<br\/>    }<br\/>    return prev;<br\/>}<br\/> <br\/>\/\/ This function populates arbit pointer in every<br\/>\/\/ node to the greatest value to its right.<br\/>Node* populateArbit(Node *head)<br\/>{<br\/>    \/\/ Reverse given linked list<br\/>    head = reverse(head);<br\/> <br\/>    \/\/ Initialize pointer to maximum value node<br\/>    Node *max = head;<br\/> <br\/>    \/\/ Traverse the reversed list<br\/>    Node *temp = head-&gt;next;<br\/>    while (temp != NULL)<br\/>    {<br\/>        \/\/ Connect max through arbit pointer<br\/>        temp-&gt;arbit = max;<br\/> <br\/>        \/\/ Update max if required<br\/>        if (max-&gt;data &lt; temp-&gt;data)<br\/>            max = temp;<br\/> <br\/>        \/\/ Move ahead in reversed list<br\/>        temp = temp-&gt;next;<br\/>    }<br\/> <br\/>    \/\/ Reverse modified linked list and return<br\/>    \/\/ head.<br\/>    return reverse(head);<br\/>}<br\/> <br\/>\/\/ Utility function to print result linked list<br\/>void printNextArbitPointers(Node *node)<br\/>{<br\/>    printf(&quot;Node\\tNext Pointer\\tArbit Pointer\\n&quot;);<br\/>    while (node!=NULL)<br\/>    {<br\/>        cout &lt;&lt; node-&gt;data &lt;&lt; &quot;\\t\\t&quot;;<br\/> <br\/>        if (node-&gt;next)<br\/>            cout &lt;&lt; node-&gt;next-&gt;data &lt;&lt; &quot;\\t\\t&quot;;<br\/>        else cout &lt;&lt; &quot;NULL&quot; &lt;&lt; &quot;\\t\\t&quot;;<br\/> <br\/>        if (node-&gt;arbit)<br\/>            cout &lt;&lt; node-&gt;arbit-&gt;data;<br\/>        else cout &lt;&lt; &quot;NULL&quot;;<br\/> <br\/>        cout &lt;&lt; endl;<br\/>        node = node-&gt;next;<br\/>    }<br\/>}<br\/> <br\/>\/* Function to create a new node with given data *\/<br\/>Node *newNode(int data)<br\/>{<br\/>    Node *new_node = new Node;<br\/>    new_node-&gt;data = data;<br\/>    new_node-&gt;next = NULL;<br\/>    return new_node;<br\/>}<br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>    Node *head = newNode(5);<br\/>    head-&gt;next = newNode(10);<br\/>    head-&gt;next-&gt;next = newNode(2);<br\/>    head-&gt;next-&gt;next-&gt;next = newNode(3);<br\/> <br\/>    head = populateArbit(head);<br\/> <br\/>    printf(&quot;Resultant Linked List is: \\n&quot;);<br\/>    printNextArbitPointers(head);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Resultant Linked List is: \r\nNode    Next Pointer    Arbit Pointer\r\n5               10              10\r\n10              2               3\r\n2               3               3\r\n3               NULL            NULL<\/pre>\n<p><strong>Recursive Solution:<\/strong><\/p>\n<p>We can recursively reach the last node and traverse the linked list from end. Recursive solution doesn\u2019t require reversing of 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.<\/p>\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to point arbit pointers to highest<br\/>\/\/ value on its right<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>\/* Link list node *\/<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    Node* next, *arbit;<br\/>};<br\/> <br\/>\/\/ This function populates arbit pointer in every<br\/>\/\/ node to the greatest value to its right.<br\/>void populateArbit(Node *head)<br\/>{<br\/>    \/\/ using static maxNode to keep track of maximum<br\/>    \/\/ orbit node address on right side<br\/>    static Node *maxNode;<br\/> <br\/>    \/\/ if head is null simply return the list<br\/>    if (head == NULL)<br\/>        return;<br\/> <br\/>    \/* if head-&gt;next is null it means we reached at<br\/>       the last node just update the max and maxNode *\/<br\/>    if (head-&gt;next == NULL)<br\/>    {<br\/>        maxNode = head;<br\/>        return;<br\/>    }<br\/> <br\/>    \/* Calling the populateArbit to the next node *\/<br\/>    populateArbit(head-&gt;next);<br\/> <br\/>    \/* updating the arbit node of the current<br\/>     node with the maximum value on the right side *\/<br\/>    head-&gt;arbit = maxNode;<br\/> <br\/>    \/* if current Node value id greater then<br\/>     the previous right node then  update it *\/<br\/>    if (head-&gt;data &gt; maxNode-&gt;data)<br\/>        maxNode = head;<br\/> <br\/>   return;<br\/>}<br\/> <br\/>\/\/ Utility function to print result linked list<br\/>void printNextArbitPointers(Node *node)<br\/>{<br\/>    printf(&quot;Node\\tNext Pointer\\tArbit Pointer\\n&quot;);<br\/>    while (node!=NULL)<br\/>    {<br\/>        cout &lt;&lt; node-&gt;data &lt;&lt; &quot;\\t\\t&quot;;<br\/> <br\/>        if(node-&gt;next)<br\/>            cout &lt;&lt; node-&gt;next-&gt;data &lt;&lt; &quot;\\t\\t&quot;;<br\/>        else cout &lt;&lt; &quot;NULL&quot; &lt;&lt; &quot;\\t\\t&quot;;<br\/> <br\/>        if(node-&gt;arbit)<br\/>            cout &lt;&lt; node-&gt;arbit-&gt;data;<br\/>        else cout &lt;&lt; &quot;NULL&quot;;<br\/> <br\/>        cout &lt;&lt; endl;<br\/>        node = node-&gt;next;<br\/>    }<br\/>}<br\/> <br\/>\/* Function to create a new node with given data *\/<br\/>Node *newNode(int data)<br\/>{<br\/>    Node *new_node = new Node;<br\/>    new_node-&gt;data = data;<br\/>    new_node-&gt;next = NULL;<br\/>    return new_node;<br\/>}<br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>    Node *head = newNode(5);<br\/>    head-&gt;next = newNode(10);<br\/>    head-&gt;next-&gt;next = newNode(2);<br\/>    head-&gt;next-&gt;next-&gt;next = newNode(3);<br\/> <br\/>    populateArbit(head);<br\/> <br\/>    printf(&quot;Resultant Linked List is: \\n&quot;);<br\/>    printNextArbitPointers(head);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Resultant Linked List is: \r\nNode    Next Pointer    Arbit Pointer\r\n5               10              10\r\n10              2               3\r\n2               3               3\r\n3               NULL            NULL<\/pre>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Cpp Algorithm-Point arbit pointer to greatest value right side node in a linked list-Linked list<br \/>\nGiven singly linked list with every node <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81795,81798,81796,70487,80236,81797,72485,79556],"class_list":["post-27587","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-add-1-to-a-number-represented-as-linked-list","tag-arbitrary","tag-distance-between-two-nodes-in-binary-tree","tag-doubly-linked-list","tag-geeksforgeeks","tag-get-the-local-maxima-or-minima-in-array","tag-linked-list-c","tag-linked-list-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27587"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27587\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}