C++ Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer
Last Updated :
09 Apr, 2025
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 nodes one by one, for every node, find the node which has the next greater value of the current node and change the next pointer. Time Complexity of this solution is O(n2).
An Efficient Solution works in O(nLogn) time. The idea is to use Merge Sort for linked list.
1) Traverse input list and copy next pointer to arbit pointer for every node.
2) Do Merge Sort for the linked list formed by arbit pointers.
Below is the implementation of the above idea. All of the merger sort functions are taken from here. The taken functions are modified here so that they work on arbit pointers instead of next pointers.
C++
// C++ program to populate arbit pointers
// to next higher value using merge sort
#include <bits/stdc++.h>
using namespace std;
// Link list node
class Node
{
public:
int data;
Node* next, *arbit;
};
// Function prototypes
Node* SortedMerge(Node* a, Node* b);
void FrontBackSplit(Node* source,
Node** frontRef,
Node** backRef);
/* Sorts the linked list formed by
arbit pointers (does not change
next pointer or data) */
void MergeSort(Node** headRef)
{
Node* head = *headRef;
Node* a, *b;
/* Base case -- length 0
or 1 */
if ((head == NULL) ||
(head->arbit == NULL))
return;
/* Split head into 'a' and
'b' sublists */
FrontBackSplit(head, &a, &b);
// Recursively sort the sublists
MergeSort(&a);
MergeSort(&b);
/* answer = merge the two sorted
lists together */
*headRef = SortedMerge(a, b);
}
/* See https://www.geeksforgeeks.org/?p=3622
for details of this function */
Node* SortedMerge(Node* a, Node* b)
{
Node* result = NULL;
// Base cases
if (a == NULL)
return (b);
else if (b == NULL)
return (a);
// Pick either a or b, and recur
if (a->data <= b->data)
{
result = a;
result->arbit =
SortedMerge(a->arbit, b);
}
else
{
result = b;
result->arbit =
SortedMerge(a, b->arbit);
}
return (result);
}
/* Split the nodes of the given list into
front and back halves, and return the
two lists using the reference parameters.
If the length is odd, the extra node
should go in the front list. Uses the
fast/slow pointer strategy. */
void FrontBackSplit(Node* source,
Node** frontRef,
Node** backRef)
{
Node* fast, *slow;
if (source == NULL ||
source->arbit == NULL)
{
// length < 2 cases
*frontRef = source;
*backRef = NULL;
return;
}
slow = source,
fast = source->arbit;
/* Advance 'fast' two nodes, and
advance 'slow' one node */
while (fast != NULL)
{
fast = fast->arbit;
if (fast != NULL)
{
slow = slow->arbit;
fast = fast->arbit;
}
}
/* 'slow' is before the midpoint
in the list, so split it in
two at that point. */
*frontRef = source;
*backRef = slow->arbit;
slow->arbit = NULL;
}
/* Function to insert a node at
the beginning of the linked list */
void push(Node** head_ref, int new_data)
{
// Allocate node
Node* new_node = new Node();
// Put in the data
new_node->data = new_data;
// Link the old list of the
// new node
new_node->next = (*head_ref);
new_node->arbit = NULL;
// Move the head to point to the
// new node
(*head_ref) = new_node;
}
// Utility function to print result
// linked list
void printListafter(Node *node,
Node *anode)
{
cout << "Traversal using Next Pointer";
while (node!=NULL)
{
cout << node->data << ", ";
node = node->next;
}
printf("Traversal using Arbit Pointer");
while (anode!=NULL)
{
cout << anode->data << ", ";
anode = anode->arbit;
}
}
// This function populates arbit pointer
// in every node to the next higher value.
// And returns pointer to the node with
// minimum value
Node* populateArbit(Node *head)
{
// Copy next pointers to arbit
// pointers
Node *temp = head;
while (temp != NULL)
{
temp->arbit = temp->next;
temp = temp->next;
}
// Do merge sort for arbitrary
// pointers
MergeSort(&head);
// Return head of arbitrary pointer
// linked list
return head;
}
// Driver code
int main()
{
// Start with the empty list
Node* head = NULL;
// Let us create the list shown
// above
push(&head, 3);
push(&head, 2);
push(&head, 10);
push(&head, 5);
// Sort the above created Linked List
Node *ahead = populateArbit(head);
cout << "Result Linked List is: ";
printListafter(head, ahead);
return 0;
}
// This is code is contributed by rathbhupendra
Output:
Result Linked List is:
Traversal using Next Pointer
5, 10, 2, 3,
Traversal using Arbit Pointer
2, 3, 5, 10,
Time Complexity: O(n log n), where n is the number of nodes in the Linked list.
Space Complexity: O(1). We are not using any extra space.
Please refer complete article on Point to next higher value node in a linked list with an arbitrary pointer for more details!
Similar Reads
C++ Program For Pointing Arbit Pointer To Greatest Value Right Side Node In A Linked List 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
5 min read
C++ Program For Partitioning A Linked List Around A Given Value And Keeping The Original Order Given a linked list and a value x, partition it such that all nodes less than x come first, then all nodes with a value equal to x, and finally nodes with a value greater than or equal to x. The original relative order of the nodes in each of the three partitions should be preserved. The partition m
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 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 For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit
2 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 To Merge A Linked List Into Another Linked List At Alternate Positions Given two linked lists, insert nodes of second list into first list at alternate positions of first list. For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become 5->12->7->10->17->2->13->4->11->6 an
3 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 Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
8 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