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++
class LinkedList
{
static Node head;
static class Node
{
int data;
Node next, arbit;
Node( int data)
{
this .data = data;
next = null;
arbit = null;
}
}
void printList(Node node, Node anode)
{
System.out.println(
"Traversal using Next Pointer" );
while (node != null)
{
System.out.print(node.data + " " );
node = node.next;
}
System.out.println(
"Traversal using Arbit Pointer" );
while (anode != null)
{
System.out.print(
anode.data + " " );
anode = anode.arbit;
}
}
private Node populateArbit(Node start)
{
Node temp = start;
while (temp != null)
{
temp.arbit = temp.next;
temp = temp.next;
}
return MergeSort(start);
}
private Node MergeSort(Node start)
{
if (start == null ||
start.arbit == null)
{
return start;
}
Node middle = getMiddle(start);
Node nextofmiddle = middle.arbit;
middle.arbit = null;
Node left = MergeSort(start);
Node right = MergeSort(nextofmiddle);
Node sortedlist = SortedMerge(left, right);
return sortedlist;
}
private Node getMiddle(Node source)
{
if (source == null)
return source;
Node fastptr = source.arbit;
Node slowptr = source;
while (fastptr != null)
{
fastptr = fastptr.arbit;
if (fastptr != null)
{
slowptr = slowptr.arbit;
fastptr = fastptr.arbit;
}
}
return slowptr;
}
private Node SortedMerge(Node a,
Node b)
{
Node result = null;
if (a == null)
return b;
else if (b == null)
return a;
if (a.data <= b.data)
{
result = a;
result.arbit =
SortedMerge(a.arbit, b);
}
else
{
result = b;
result.arbit = SortedMerge(a, b.arbit);
}
return result;
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(5);
list.head.next = new Node(10);
list.head.next.next = new Node(2);
list.head.next.next.next = new Node(3);
Node ahead = list.populateArbit(head);
System.out.println( "Result Linked List is:" );
list.printList(head, ahead);
}
}
|
Output:
Result Linked List is:
Traversal using Next Pointer
5, 10, 2, 3,
Traversal using Arbit Pointer
2, 3, 5, 10,
The time complexity of the algorithm is O(n log n), where n is the length of the input linked list.
The space complexity of the algorithm is O(log n), where n is the length of the input linked list.
Please refer complete article on Point to next higher value node in a linked list with an arbitrary pointer for more details!