Python Program For Pointing Arbit Pointer To Greatest Value Right Side Node In A Linked List
Last Updated :
03 May, 2023
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 which has the greatest value on the right side and change the next pointer.
Below is the implementation of the above approach:
Python3
# Python Program to point arbit pointers
# to highest value on its right
# Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
self.arbit = None
# Function to reverse the linked list
def reverse(head):
prev = None
current = head
next = None
while (current != None):
next = current.next
current.next = prev
prev = current
current = next
return prev
# This function populates arbit pointer
# in every node to the greatest value
# to its right.
def populateArbit(head):
# Reverse given linked list
head = reverse(head)
# Initialize pointer to maximum value node
max = head
# Traverse the reversed list
temp = head.next
while (temp != None):
# Connect max through arbit pointer
temp.arbit = max
# Update max if required
if (max.data < temp.data):
max = temp
# Move ahead in reversed list
temp = temp.next
# Reverse modified linked list and return head.
return reverse(head)
# Utility function to print result linked list
def printNextArbitPointers(node):
print("Node ", "Next Pointer " , "Arbit Pointer")
while (node != None):
print(node.data , " ", end = "")
if (node.next != None):
print(node.next.data , " ", end = "")
else :
print("NULL" , " ",end = "")
if (node.arbit != None):
print(node.arbit.data, end = "")
else :
print("NULL", end = "")
print("")
node = node.next
# Function to create a new node with given data
def newNode(data):
new_node = Node(0)
new_node.data = data
new_node.next = None
return new_node
# Driver code
head = newNode(5)
head.next = newNode(10)
head.next.next = newNode(2)
head.next.next.next = newNode(3)
head = populateArbit(head)
print("Resultant Linked List is: ")
printNextArbitPointers(head)
# This code is contributed by Susobhan Akhuli
OutputResultant Linked List is:
Node Next Pointer Arbit Pointer
5 10 10
10 2 3
2 3 3
3 NULL NULL
The Time Complexity of this above solution is O(n2).
Space complexity:
The space complexity of the program is O(1) as it uses a constant amount of extra space irrespective of the input size.
An Efficient Solution can work in O(n) time. Below are the steps.
- Reverse the given linked list.
- Start traversing the linked list and store the maximum value node encountered so far. Make arbit of every node to point to max. If the data in the current node is more than the max node so far, update max.
- Reverse modified linked list and return head.
Following is the implementation of the above steps.
Python
# Python Program to point arbit pointers
# to highest value on its right
# Node class
class Node:
# Constructor to initialize the
# node object
def __init__(self, data):
self.data = data
self.next = None
self.arbit = None
# Function to reverse the linked list
def reverse(head):
prev = None
current = head
next = None
while (current != None):
next = current.next
current.next = prev
prev = current
current = next
return prev
# This function populates arbit pointer
# in every node to the greatest value
# to its right.
def populateArbit(head):
# Reverse given linked list
head = reverse(head)
# Initialize pointer to maximum
# value node
max = head
# Traverse the reversed list
temp = head.next
while (temp != None):
# Connect max through arbit
# pointer
temp.arbit = max
# Update max if required
if (max.data < temp.data):
max = temp
# Move ahead in reversed list
temp = temp.next
# Reverse modified linked list and
# return head.
return reverse(head)
# Utility function to print result
# linked list
def printNextArbitPointers(node):
print("Node ",
"Next Pointer " ,
"Arbit Pointer")
while (node != None):
print(node.data ,
" ",
end = "")
if (node.next != None):
print(node.next.data ,
" ", end = "")
else :
print("NULL" ,
" ",end = "")
if (node.arbit != None):
print(node.arbit.data, end = "")
else :
print("NULL", end = "")
print("")
node = node.next
# Function to create a new node
# with given data
def newNode(data):
new_node = Node(0)
new_node.data = data
new_node.next = None
return new_node
# Driver code
head = newNode(5)
head.next = newNode(10)
head.next.next = newNode(2)
head.next.next.next = newNode(3)
head = populateArbit(head)
print("Resultant Linked List is: ")
printNextArbitPointers(head)
# This code is contributed by Arnab Kundu
# This code is modified by Susobhan Akhuli
OutputResultant Linked List is:
Node Next Pointer Arbit Pointer
5 10 10
10 2 3
2 3 3
3 NULL NULL
The time complexity of this program is O(n), where n is the number of nodes in the linked list.
The space complexity of this program is also O(n), as it creates a new node for each element in the list and stores them in memory.
Recursive Solution:
We can recursively reach the last node and traverse the linked list from the end. The recursive solution doesn’t require reversing of the 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.
Python3
# Python3 program to point arbit pointers
# to highest value on its right
''' Link list node '''
# Node class
class newNode:
# Constructor to initialize the
# node object
def __init__(self, data):
self.data = data
self.next = None
self.arbit = None
# This function populates arbit pointer
# in every node to the greatest value
# to its right.
maxNode = newNode(None)
def populateArbit(head):
# using static maxNode to keep track
# of maximum orbit node address on
# right side
global maxNode
# if head is null simply return the list
if (head == None):
return
''' if head.next is null it means we
reached at the last node just update
the max and maxNode '''
if (head.next == None):
maxNode = head
return
''' Calling the populateArbit to the
next node '''
populateArbit(head.next)
''' updating the arbit node of the
current node with the maximum
value on the right side '''
head.arbit = maxNode
''' if current Node value id greater
then the previous right node then
update it '''
if (head.data > maxNode.data and
maxNode.data != None ):
maxNode = head
return
# Utility function to prresult
# linked list
def printNextArbitPointers(node):
print("Node " ,
"Next Pointer " ,
"Arbit Pointer")
while (node != None):
print(node.data,
" ",
end = "")
if(node.next):
print(node.next.data,
" ",
end = "")
else:
print("NULL", " ",
end = "")
if(node.arbit):
print(node.arbit.data, end = "")
else:
print("NULL", end = "")
print()
node = node.next
# Driver code
head = newNode(5)
head.next = newNode(10)
head.next.next = newNode(2)
head.next.next.next = newNode(3)
populateArbit(head)
print("Resultant Linked List is:")
printNextArbitPointers(head)
# This code is contributed by SHUBHAMSINGH10
OutputResultant Linked List is:
Node Next Pointer Arbit Pointer
5 10 10
10 2 3
2 3 3
3 NULL NULL
Please refer complete article on Point arbit pointer to greatest value right side node in a linked list for more details!
Similar Reads
Python Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer
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 n
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 Removing Middle Points From a Linked List Of Line Segments
Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked
4 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 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
Python Program To Delete Alternate Nodes Of A Linked List
Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
3 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 find middle of a linked list using one traversal
Given a singly linked list, find the middle of the linked list. Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. Method 1: Traverse the whole linked list and count the no. of nodes. Now tra
5 min read
Python Program For Finding Intersection Of Two Sorted Linked Lists
Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2->4->6->8, Ou
4 min read