Python 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.
Python3
# Python3 program to populate arbit pointers
# to next higher value using merge sort
head = None
# Link l node
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.arbit = None
# Utility function to print result
# linked l
def printList(node, anode):
print("Traversal using Next Pointer")
while (node != None):
print(node.data, end = ", ")
node = node.next
print("Traversal using Arbit Pointer");
while (anode != None):
print(anode.data, end = ", ")
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
def populateArbit(start):
temp = start
# Copy next pointers to arbit pointers
while (temp != None):
temp.arbit = temp.next
temp = temp.next
# Do merge sort for arbitrary pointers and
# return head of arbitrary pointer linked l
return MergeSort(start)
# Sorts the linked l formed by arbit pointers
# (does not change next pointer or data)
def MergeSort(start):
# Base case -- length 0 or 1
if (start == None or start.arbit == None):
return start
# Split head into 'middle' and
# 'nextofmiddle' sublists
middle = getMiddle(start)
nextofmiddle = middle.arbit
middle.arbit = None
# Recursively sort the sublists
left = MergeSort(start)
right = MergeSort(nextofmiddle)
# answer = merge the two sorted lists together
sortedlist = SortedMerge(left, right)
return sortedlist
# Utility function to get the
# middle of the linked l
def getMiddle(source):
# Base case
if (source == None):
return source
fastptr = source.arbit
slowptr = source
# Move fastptr by two and slow ptr by one
# Finally slowptr will point to middle node
while (fastptr != None):
fastptr = fastptr.arbit
if (fastptr != None):
slowptr = slowptr.arbit
fastptr = fastptr.arbit
return slowptr
def SortedMerge(a, b):
result = None
# Base cases
if (a == None):
return b
elif (b == None):
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
if __name__=='__main__':
# Let us create the l shown above
head = Node(5)
head.next = Node(10)
head.next.next = Node(2)
head.next.next.next = Node(3)
# Sort the above created Linked List
ahead = populateArbit(head)
print("Result Linked List is:")
printList(head, ahead)
# This code is contributed by rutvik_56
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
Python 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
7 min read
Python Program For Cloning A Linked List With Next And Random Pointer In O(1) Space Given a linked list having two pointers in each node. The first one points to the next node of the list, however, the other pointer is random and can point to any node of the list. Write a program that clones the given list in O(1) space, i.e., without any extra space. Examples: Input : Head of the
4 min read
Python 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
Python Program For Cloning A Linked List With Next And Random Pointer- Set 2 We have already discussed 2 different ways to clone a linked list. In this post, one more simple method to clone a linked list is discussed. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. The idea is to use Hashing. Below is algorithm. Traverse the original link
3 min read
Python 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
Python 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
Python 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->
4 min read
Python Program To Merge A Linked List Into Another Linked List At Alternate Positions Given two linked lists, insert nodes of the second list into the first list at alternate positions of the 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-
3 min read
Python 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
Python Program To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples: Input: M = 2, N = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked L
3 min read