Java Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer
Last Updated :
20 Mar, 2023
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++
[tabby title="Java"][sourcecode language="Java"]
// Java program to populate arbit pointers
// to next higher value using merge sort
class LinkedList
{
static Node head;
// Link list node
static class Node
{
int data;
Node next, arbit;
Node(int data)
{
this.data = data;
next = null;
arbit = null;
}
}
// Utility function to print result
// linked list
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;
}
}
// This function populates arbit pointer
// in every node to the next higher value.
// And returns pointer to the node with
// minimum value
private Node populateArbit(Node start)
{
Node temp = start;
// Copy next pointers to arbit
// pointers
while (temp != null)
{
temp.arbit = temp.next;
temp = temp.next;
}
// Do merge sort for arbitrary pointers
// and return head of arbitrary pointer
// linked list
return MergeSort(start);
}
/* Sorts the linked list formed by
arbit pointers (does not change
next pointer or data) */
private Node MergeSort(Node start)
{
// Base case -- length
// 0 or 1
if (start == null ||
start.arbit == null)
{
return start;
}
/* Split head into 'middle' and
'nextofmiddle' sublists */
Node middle = getMiddle(start);
Node nextofmiddle = middle.arbit;
middle.arbit = null;
// Recursively sort the sublists
Node left = MergeSort(start);
Node right = MergeSort(nextofmiddle);
/* answer = merge the two sorted
lists together */
Node sortedlist = SortedMerge(left, right);
return sortedlist;
}
// Utility function to get the middle
// of the linked list
private Node getMiddle(Node source)
{
// Base case
if (source == null)
return source;
Node fastptr = source.arbit;
Node slowptr = source;
// Move fastptr by two and slow
// ptr by one. Finally slowptr
// will point to middle node
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;
// 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;
}
// Driver code
public static void main(String[] args)
{
LinkedList list = new LinkedList();
/* Let us create the list shown
above */
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);
/* Sort the above created Linked List */
Node ahead = list.populateArbit(head);
System.out.println("Result Linked List is:");
list.printList(head, ahead);
}
}
// This code is contributed by shubham96301
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!
Similar Reads
Java 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
Java 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 the list and n = 3, then the output is "B". Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1 (Use
5 min read
Java Program For Writing A Function To Get Nth Node In A Linked List Write a 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 = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th
4 min read
Java Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1
3 min read
Java Program For Printing Nth Node From The End Of A Linked List(Duplicate) 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
2 min read
Java Program to Insert a New Node at the Beginning of the Circular Linked List Circular linked list: A circular linked list is a sequence of elements in which every element points to its next element in the sequence and the last element has a link to the first element. That means a circular linked list is similar to the single linked list except that the last node points to th
4 min read
Java Program to Insert a New Node at the Middle of the Circular Linked List Given a Circular Linked List, the task is to add a New Node at the Middle of the List. Let's consider the following Circular Linked List: List-Before-InsertionList-After-InsertionCreate a new node (New_node).Check for an empty list. If the list is empty then insert the node as head.For non-empty lis
3 min read
Java 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-
6 min read
Java 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
Java Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. Java // Linked List Class class LinkedList { // Head of list Node
7 min read